$str = "()"; if( $str =~ s/(?{ print $1, "\n" })/<\/ref>/ ){ print "Match was found." . "\n"; } print $str . "\n";