in reply to Contents b/w two given text

Or, expanded to have your texts taken from variables:

#!/usr/local/bin/perl -w use strict; my $string = "blah text1 you mean something like this? text2 blah blah +"; my $t1 = 'text1'; my $t2 = 'text2'; my $match = $t1 . '(.*?)' . $t2; if ($string =~ m/$match/s) { print $1; }

Replies are listed 'Best First'.
Re^2: Contents b/w two given text
by halley (Prior) on Aug 25, 2005 at 13:17 UTC
    Unless you are certain that $t1 and $t2 have no special regular expression characters, like [ ] . * ? + ( ) \ ^ $ ? and so on, you should clip them with \Q \E.
    #!/usr/local/bin/perl -w use strict; my $string = "blah text1 you mean more like this? text2 blah"; my $t1 = 't(e*x+t)1'; my $t2 = 't[ex]*t?2'; my $match = qr/\Q$t1\E(.*?)\Q$t2/; print "no match\n" unless $string =~ m/$match/s;

    --
    [ e d @ h a l l e y . c c ]