Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching all words except one

Tags:

regex

Say I have a sentence:

I am a good buy and bad boy too

How to select every word except boy in this sentence using regular expression ?

like image 842
pokrate Avatar asked Jan 21 '10 09:01

pokrate


People also ask

How do you exclude a word in regex?

If you want to exclude a certain word/string in a search pattern, a good way to do this is regular expression assertion function. It is indispensable if you want to match something not followed by something else. ?= is positive lookahead and ?! is negative lookahead.

How do I not match a character in regex?

To replace or remove characters that don't match a regex, call the replace() method on the string passing it a regular expression that uses the caret ^ symbol, e.g. /[^a-z]+/ .

What does ?! Mean in regex?

It's a negative lookahead, which means that for the expression to match, the part within (?!...) must not match. In this case the regex matches http:// only when it is not followed by the current host name (roughly, see Thilo's comment). Follow this answer to receive notifications.

What does * do in regex?

The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.


4 Answers

You can use negative look behind:

\w+\b(?<!\bboy)

Or negative look ahead since not all support negative look behind

(?!boy\b)\b\w+

You can read about negative look ahead here

like image 118
Jeremy Seekamp Avatar answered Oct 09 '22 07:10

Jeremy Seekamp


Try:

\b(?!boy\b).*?\b

which means:

  • Zero width word break (\b)
  • That isn't followed by "boy" and another word break;
  • followed by any characters in a non-greedy way;
  • Up until another word break.

Note: the word break matches the start of the string, the end of the string and any transition from word (number, letter or underscore) to non-word character or vice versa.

like image 23
cletus Avatar answered Oct 09 '22 08:10

cletus


/\b(?!boy)\S+/g
like image 21
kennytm Avatar answered Oct 09 '22 07:10

kennytm


If you use "boy" as splitter, you would get remaining parts. You could use those as selection keys.

>>> re.split("boy","I am a good buy and bad boy too")
['I am a good buy and bad ', ' too']
like image 27
YOU Avatar answered Oct 09 '22 08:10

YOU