void techBlog() { //... }


My attemps to arrange 0 and 1 and make something vaguely useful


How to negate a regexp with a negative lookahead

When I need it, I tend to forget how to create a regular expression that matches a large set of strings but does not match some specific strings. It’s tricky because it involves the negative lookahead syntax.

For exemple the regexp

.+\.java

matches any java filename.

But the following regexp

(?!Foo\.java).+\.java

matches any java filename except Foo.java because the negative lookahead of Foo\.java will prevent .+\.java from matching.

The negative lookahead syntax is

(?!X) where X is the string that must not be matched.

comments powered by Disqus