I would like to match everything except the given word(s), so given this list:
wordOne
wordTwo/xy/z
word-three
word-four/lots/of/stuff
I could use this regexp to match everything except wordOne:
(?!wordOne)\b.+
==>
wordTwo/xy/z
word-three
word-four/lots/of/stuff
However if I want to match everything except one of the words containing a hyphen/dash, the same regexp does not work, because the hyphen is not a part of the word boundary - which is [a-zA-Z0-9_]
E.g
some-regexp(word-four)
==>
wordOne
wordTwo/xy/z
word-three
And
some-regexp(word-four and word-three)
==>
wordOne
wordTwo/xy/z
use "\p{Pd}" without quotes to match any type of hyphen. The '-' character is just one type of hyphen which also happens to be a special character in Regex.
You only need to escape the dash character if it could otherwise be interpreted as a range indicator (which can be the case inside a character class).
As I can see you define one word per line in your examples. In this case this regex should work for you:
^(?:(?!word-four|word-three).)*$
it skips the words which contain word-four
or word-three
.
As per your examples:
^(?:(?!wordOne).)*$
==>
wordTwo/xy/z
word-three
word-four/lots/of/stuff
^(?:(?!word-four).)*$
==>
wordTwo/xy/z
word-three
word-four/lots/of/stuff
^(?:(?!word-four|word-three).)*$
==>
wordOne
wordTwo/xy/z
See it on rubular.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With