>perl -wMstrict -le "my $s = 'ABC1D A2D D3A foo4bar'; my @matches = $s =~ m{ A (?: BC)? (\d+) D }xmsg; print qq{'$_'} for @matches; " '1' '2'