Skip to content

Instantly share code, notes, and snippets.

@mence
Created July 10, 2015 22:24
Show Gist options
  • Save mence/bc989877b29d588ab99c to your computer and use it in GitHub Desktop.
Save mence/bc989877b29d588ab99c to your computer and use it in GitHub Desktop.
How to exclude exact strings using a regular expression

Negative Lookaround

Does not match foo

^((?!foo).)*$

Result:

foo
bar <- match
baz <- match
qux <- match
foobar
barbaz <- match

Exact string matcher

Matches exactly foo, bar

((^|, )(foo|bar))+$

Result:

foo <- match
bar <- match
baz
qux
foobar
barbaz

Does not match the exact strings of foo, bar

^((?!((^|, )(foo|bar))+$).)*$

foo
bar
baz <- match
qux <- match
foobar <- match
barbaz <- match

Useful tools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment