my $str = "start b2 end start b2 b2 end start b2 end"; if ($str =~ /.*start((.*?b2.*?b2).*?)end/) { print "$1 is between \"start\" and \"end\"\n"; }