1

I have a few strings:

1. "I offer a jacket from adidas"
2. "I offer clothing from adidas, a jacket"
3. "By my adidas jacket"
4. "adidas jacket"
5. "jacket adidas"
6. "I offer some shoes from adidas"
7. "Selling some nice adidas shoes"
8. "I have some shoes from adidas"

Now I want a hit, when the word "adidas" is found, but not when somewhere in the string "jacket" is included.

I manage to get a non-hit when the word "jacket" is behind the word "adidas", but I get a hit, when the word "jacket" is in front of "adidas".

adidas(?!.*jacket.*)

shows hits for 1., 5., 6., 7. and 8. But 1 and 5 shouldn't be shown as hit.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70

1 Answers1

1

You could place the negative lookahead at the start and use an anchor ^ and word boundaries \b to prevent the word being part of a larger word.

^(?!.*\bjacket\b).*\badidas\b.*
  • ^ Start of string
  • (?!.*\bjacket\b) Assert what is on the right does not contain jacket
  • .*\badidas\b.* Match 0+ times any char, then match adidas and 0+ times any char

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70