41

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 ?

pokrate
  • 3,954
  • 7
  • 30
  • 36

8 Answers8

67

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

Jeremy Seekamp
  • 2,746
  • 1
  • 22
  • 25
15

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.

cletus
  • 616,129
  • 168
  • 910
  • 942
5
/\b(?!boy)\S+/g
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
2

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']
YOU
  • 120,166
  • 34
  • 186
  • 219
1

As alternative, if possessive quantifiers are available, you can use;

\w+(?<!boy)

It takes generally fewer steps than using world boundaries.

Jimmy Zhao
  • 303
  • 2
  • 17
0

Substitute boy to nothing... in Perl that would be:

s/boy //g
Niels Castle
  • 8,039
  • 35
  • 56
0

I tested the following with http://regex101.com:

\b(?!boy)\S+|\w*\b$

This provides the list of all words delimited by spaces, but excludes only the word "boy" as asked.

justdan23
  • 560
  • 7
  • 9
-1

Which language? Why do you want to use a regex?

answer = yourString.Replace( "boy", "" );
tanascius
  • 53,078
  • 22
  • 114
  • 136
  • 6
    Silly because? The spec is not clear and this solution matches one of the interpretations. – Grzegorz Oledzki Jan 21 '10 at 09:19
  • 3
    Note the OP said "word". This will match partials. So if you wanted to ignore "top" and you did this replace you'd also hit "stopped". So this answer is silly because it doesn't an *unnecessary* replacement that doesn't solve the problem, ignores the issue of word boundaries and just introduces more problems. – cletus Jan 21 '10 at 10:16
  • It doesn't use a Regex - that seems to be a problem for you. In my opinion you do not have to use a Regex in all cases. Here is a solution, that does not. You can find wordboundaries by including spaces. This answer might be helpful to the OP since it introduces a new possibility without Regex. But the OP is free to accept whatever answer fits best to his requirements. – tanascius Jan 21 '10 at 10:35
  • 1
    Regex or non-regex, I don't care so long as it works. Simple string replacement when looking for **words** clearly does not. – cletus Jan 21 '10 at 15:46
  • This led me on the right path...use regex to match the entire string I need, and then use replace function with regex (in PHP) to remove the parts within it which I wanted to exclude. +1 – blizz Apr 16 '17 at 01:16
  • sure. but this answer belonged in another question or as a comment. Original Poster - "using regular expression" – TamusJRoyce Jun 29 '21 at 11:32