my @x = ( 'smomedef12345', 'anabc12345 and there is some def12345678', 'qwerabc12345def55', 'def87654321abc54321', ); print map{ /abc\d{5}/ and /def(\d{8})/ ? "def=$1\n" : () }@x; __END__ prints: def=12345678 def=87654321