Sunday, January 17, 2010

Negate expressions using regex

a not so rare question using regex is "match all strings doesn't contain word foo". that isn't something regex is made for. by the way searching the web shows that the expression
(?:(?!REGEX).)*
where the expression REGEX must be replace with expression must be negated, e.g. (?:(?!(foo1|foo2)).)* returns true for all strings doesn't contain foo1 OR foo2.

more details and background can be found here: http://www.perlmonks.org/?node_id=588315#588368

No comments: