in reply to Re: Regex question - negatives
in thread Regex question - negatives

Hi,

Thanks - just having a look into those negative look-ahead stuff, as I'm sure that will be good to know for other regexes.

You example doesn't seem to work?
\[\[ # opening delimiter ( # capture... (?: # a group (?!\]\]) # that does not start with ]] . # and is a single character long )+ # and many of these groups, at least one. ) ]] # closing delimiter
"Substitution replacment not terminated at line24"

any ideas?

TIA

Andy

Replies are listed 'Best First'.
Re^3: Regex question - negatives
by ELISHEVA (Prior) on Jan 13, 2011 at 10:43 UTC

    moritz's example is meant to be used with the x modifier which permits comments in a regex. For example:

    my $re= qr{\[\[ # opening delimiter ( # capture... (?: # a group (?!\]\]) # that does not start with ]] . # and is a single character long )+ # and many of these groups, at least one. ) ]] # closing delimiter }x; while ($test =~ m{$re}g) { print "FOO:<$1>\n"; }
      Hi,

      Ah cool, didn't know about that feature either =)

      Seems it doesn't wanna work with a s/// though?

      This works fine, and prints out all the tag I expected:
      while ($test =~ m{$re}g) { print "FOO:<$1>\n"; }
      ..while this only prints out the "test new line" one for some reason?
      $test =~ s{$re}{ print "BLA: $1 \n"; }ge
      EDIT, never mind - me being stupid with my syntax ;)

      TIA

      Andy
        ..while this only prints out the "test new line" one for some reason?

        while ($test =~ s{$re}{}ge) {
            print "BLA: $1 \n";
        }

        That is because s///g ignores context and just does all the replacements at once leaving the last match in $1.

        But m//g in scalar context will iterate through each match every time it is called in scalar context so that in a while loop $1 will contain each match in turn.

        Those "ah cool, I didn't know about that" moments are one reason I hang out around here.

        And you thought you were just going to get another set of eyes on your code....see Don't go all PerlMonks on me :-)