Question

I wanted to know, how do I capture any word which is not exactly "yellow". e.g.: "red".

I have tried the following regex:

(?!(yellow))

Debuggex Demo

... but I have two problems:

  1. It does not capture (creates a backreference) for matching expression (e.g.: "red")
  2. "yellowwwwww" does not match my regex, even though it is different from "yellow".

Can you help me correcting my regex? Thanks!

Was it helpful?

Solution 2

To capture any word which is NOT yellow you can use:

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

Online Demo: https://www.debuggex.com/r/S-qTilRd3WJqUP6x

OTHER TIPS

How about

(?!yellow$)

The inner parenthesis in your original regex are needed only if you want to capture the group

The $ ensures that only yellow will not match. If you want to use it inside a phrase use this instead

(?!yellow\b)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top