Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match everything except the given words (which may include hyphens/dashes)

Tags:

regex

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
like image 228
Paul Avatar asked Apr 05 '11 17:04

Paul


People also ask

How do you match a hyphen in regex?

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.

Do dashes need to be escaped 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).


1 Answers

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.

like image 186
Oleks Avatar answered Sep 21 '22 08:09

Oleks