Hi,
Is there a way to use the "all characters except for" character in a Regex, but rather than exclude individual characters, exclude full words.
For example, [^269] will pick up all characters except for the number 2, 6, and 9.
But what if I wanted to exclude a word? For example [^"hello"], which will detect *anything* other than the word "hello".
Can it be done?
Thanks.
Callum.
Using the 'exclusion' character [^x] for a full word
Moderator: General Moderators
- CoderGoblin
- DevNet Resident
- Posts: 1425
- Joined: Tue Mar 16, 2004 10:03 am
- Location: Aachen, Germany
You need a negative lookahead (zero width assertion). The follow sub-expession matches (not captures) any string that starts on a word boundary, ends on a word boundry and only contains the lowercase characters a-z. It is preceeded by a negative lookahead which returns false if the word is a lowercase "hello".
Code: Select all
(?:(?!\bhello\b)\b[a-z]+\b)