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

If you want to match the "hello" part of "hello"you", then you need to cause the regex to not be greedy. Try this   /"([^"]*?)"/

Replies are listed 'Best First'.
Re: Re: Problem with regex from Learning Perl (3rd edition)
by slocate (Novice) on Mar 08, 2002 at 04:47 UTC
    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"; } }
      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