Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Look-Arounds in Regexes are Hard (Common Use of a Negative Lookahead)

by ikegami (Pope)
on Jul 24, 2009 at 08:42 UTC ( #782882=note: print w/ replies, xml ) Need Help??


in reply to Look-Arounds in Regexes are Hard

I view

/(?:(?!$re).)*/s

as the equivalent of

/[^$chars]*/

except it can prevent the match of entire regexps instead of a choice of characters. I call it the common use of a negative lookahead. If you're using a negative lookahead, that construct will probably satisfy your needs.

Keep in mind that both expressions can successfully match 0 characters if not properly anchored.

I think it would be nice if /(?^$re)/ could be added as a shortcut to /(?:(?!$re)(?s:.)*/. It would be more readable, and it would help prevent the common misuse of negative lookahead (/(?!$re.*)/).

Update: Formatting.


Comment on Re: Look-Arounds in Regexes are Hard (Common Use of a Negative Lookahead)
Select or Download Code
Re^2: Look-Arounds in Regexes are Hard (Common Use of a Negative Lookahead)
by moritz (Cardinal) on Jul 24, 2009 at 09:06 UTC
    I really like that idea.

    There's just a minor point of bikeshedding: should it be (?s:.) instead of a simple dot?

    I can argue both ways: negated character classes include the newline (unless it's explicitly listed as excluded), and it becomes more predictable that way.

    On the other hand you gain more flexibility if respect the /s setting of the outer regex, allowing the caller to tweak its behaviour accordingly.

    The first point sounds more convincing to me, but I have to think a bit more about it.

      Fixed.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (15)
As of 2015-07-01 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (17 votes), past polls