in reply to How do i get string between

Others have pointed out problems with your code.

Here is another way to do it:

use strict; use warnings; my $item1 = 'this is a test /test123?'; my ($content) = $item1 =~ m{ / (.*) \? }x; print "$content\n" if defined $content;

Replies are listed 'Best First'.
Re^2: How do i get string between
by GrandFather (Saint) on May 04, 2010 at 05:09 UTC

    Better to test the match result. Consider:

    #!/usr/bin/perl use strict; use warnings; for my $str ('Matched', 'No match') { $str =~ /(\w*ed)/; print "defined: Matched $1 for '$str'\n" if defined $1; } for my $str ('Matched', 'No match') { print "match: Matched $1 for '$str'\n" if $str =~ /(\w*ed)/; }

    Prints:

    defined: Matched Matched for 'Matched' defined: Matched Matched for 'No match' match: Matched Matched for 'Matched'
    True laziness is hard work