Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: RegEx: How to negate more than one character?

by Sidhekin (Priest)
on Mar 26, 2007 at 00:15 UTC ( [id://606503]=note: print w/replies, xml ) Need Help??


in reply to RegEx: How to negate more than one character?

How can I write code that means match anything that does not contain, for instance, "ah" or "bh"? Would this work:
[^(ah|bh)]+

Your question is not as simple as it seems. Strings of characters aren't quite interchangeable with characters. There are several semantics availible that all reduce to the same case for single-character strings.

This one may match, as the first character of the match, an "h" from "ah" or "bh":

qr/(?:(?!ah|bh).)+/s;

This one may match, as the last character of the match, an "a" or "b" from "ah" or "bh":

qr/(?:.(?<!ah)(?<!bh))+/s;

This one may match neither of the above; for instance, only "xx" will be matched in "bhxxah":

qr/(?:(?!ah|bh).(?<!ah)(?<!bh))+/s;

This one may match either or both (or, with /g, all) of the above, for instance "b", "hxxa", and/or "h" from "bhxxah":

qr/(?!ah|bh).(?:.(?<!ah)(?<!bh))*|(?:a|b)/s;

And that's just for simple two-character strings ...

print "Just another Perl ${\(trickster and hacker)},"
The Sidhekin proves Sidhe did it!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found