문제

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!

도움이 되었습니까?

해결책 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

다른 팁

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top