Tim
Tim

Reputation: 99586

How to match all strings other than a particular one

To match all characters except vowels, we can use [^aeiou].

I wonder

Thanks.

Upvotes: 0

Views: 120

Answers (1)

zx81
zx81

Reputation: 41848

1. How to match all strings other than a particular one

^(?!your_string$).*$

2. How to match all strings other than a few strings

^(?!(?:string1|string2|string3)$).*$

How does that work?

  1. The idea is to use a negative lookahead (?! to check that the string does not consists solely of the string(s) to avoid. If the negative lookahead (which is an assertion) succeeds, the .*$ matches everything to the end of the string.
  2. Note the use of the ^ anchor at the beginning to ensure we are positioned at the beginning of the string.
  3. Note the $ anchor inside the negative lookahead to ensure that we are excluding your_string if it is indeed the whole string, but that we do not exclude your_string and more

Reference

  1. Mastering Lookahead and Lookbehind
  2. Negative Lookaheads

Upvotes: 3

Related Questions