in reply to Re: Problem with regex from Learning Perl (3rd edition)
in thread Problem with regex from Learning Perl (3rd edition)

The thing is that the regex (/"([^"]*?)"/) is *not* supposed to match for instance "this"string", as it is, quotes and all. On the other hand, "string" should always match... This is the regex in action...
#!/usr/bin/perl -w while (<>) { chomp; if (/"([^"]*)"/) { print "Matched: |<$&>|\n"; } else { print "No Match.\n"; } }

Replies are listed 'Best First'.
Re: Re: Re: Problem with regex from Learning Perl (3rd edition)
by Juerd (Abbot) on Mar 08, 2002 at 09:47 UTC
    It IS supposed to match.

    q/"this"string"/ =~ m{ " # X ( [^"]*? # XXXX ) " # X }x

    Remember why "foo" =~ /o/? It's because there has to be an o in there, but it's location in the string has not been fixed in the regex. Anchoring avoids this problem. ^ matches the null string at the beginning of a line or string, $ matches the null string at the end of a line (right before \n) or string.

    "foo" !~ /^o$/; q/"this"string"/ !~ /^"([^"]*?)"$/; # X X

    ++ vs lbh qrpbqrq guvf hfvat n ge va Crey :)
    Nabgure bar vs lbh qvq fb jvgubhg ernqvat n znahny svefg.
    -- vs lbh hfrq OFQ pnrfne ;)
        - Whreq