#Each line of $text is independent of the other and not joined as a continuous line. $text = q~ adfRadfaUYBGsQWERaeYETEWoyMSn nbPOIVcRCVVfOOPQbHbnRIIqWweRT ~; $result = ""; while( $text =~ /[a-z]+[A-Z]{4}([a-z]{1})[A-Z]{4}[a-z]+/g) { $result .= $1; } print $result; # prints sc but should print scf