Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Negating Regexes: Tips, Tools, And Tricks Of The Trade

by ikegami (Pope)
on Dec 07, 2006 at 16:33 UTC ( #588388=note: print w/ replies, xml ) Need Help??


in reply to Re: Negating Regexes: Tips, Tools, And Tricks Of The Trade
in thread Negating Regexes: Tips, Tools, And Tricks Of The Trade

Very good, but that can't be embedded into another regexp nicely. The equivalent to /[^$chars]*/ is /(?:(?!$re).)*/.

/[^$chars]*/ matches as many characters as possible, as long as none match the character class.
/(?:(?!$re).)*/ matches as many characters as possible, as long as no subsequence of them match the regexp.

A "few" example uses: Re^3: reg ex NOT, Re^3: Text::Balanced with nested / custom brackets, Re^2: regex for negating a sequence, Re: Regexp: Match anything except a certain word, Re^2: Regexp: Match anything except a certain word, Re: regex help please, Re: Extraneous behaviour of match variables, Re: How to split into paragraphs?, Re^3: How to split into paragraphs?, Re: text extraction question, etc.

Notes:

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

  • * can be replaced with other modifiers, like +, *?, {2}, etc.

  • This should be in the docs since it's a FAQ.

  • I think it would be nice if /(?:(?!$re).)*/ could be shortcutted to /(?^$re)/. It would be more readable, and it would help prevent the commmon misuse of negative lookahead (/(?!$re.*)/).

Updated: Fixed formatting, added stuff


Comment on Re^2: Negating Regexes: Tips, Tools, And Tricks Of The Trade
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (13)
As of 2014-08-21 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (135 votes), past polls