Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Regex question - negatives

by moritz (Cardinal)
on Jan 13, 2011 at 09:43 UTC ( [id://882078]=note: print w/replies, xml ) Need Help??


in reply to Regex question - negatives

Can someone suggest to me what I'm doing wrong?

The .+ looks wrong, since it can match [[ and ]] too. You should use [^\]]+ in the first place.

Update: Here's a working regex:

\[\[([^\]]+)]]

But it stops at a single ]. To prevent that, you need a negative look-ahead:

\[\[((?:(?!\]\]).)+)]]

Now that's quite unreadable, so here in detail:

\[\[ # 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

Replies are listed 'Best First'.
Re^2: Regex question - negatives
by JavaFan (Canon) on Jan 13, 2011 at 10:52 UTC
    To prevent that, you need a negative look-ahead:
    No, you don't.
    $_ = "[[foo]] bar [[baz ] qux]] [quux]] fred [[waldo]]]"; say $1 while /\[\[([^]]*(?:\][^]]+)*)\]\]/g; __END__ foo baz ] qux waldo
    This uses some classical loop-unrolling, and is, IIRC, similar how Mastering Regular Expressions (first edition) matches C comments.
Re^2: Regex question - negatives
by ultranerds (Hermit) on Jan 13, 2011 at 09:49 UTC
    Hi,

    Aaah ok - so I was calling it AFTER, which basically meant that it wasn't matching the string at the point I was expecting :) Thanks for that!

    This works fine now:
    $test =~ s{\[\[([^\]]+)\]}{ print "FOO: $1 \n"; }ge;
    Thanks again!

    Andy
Re^2: Regex question - negatives
by ultranerds (Hermit) on Jan 13, 2011 at 10:24 UTC
    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

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://882078]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found