Is there a possibility to write a regex that matches for [a-zA-Z]{2,4}
but not for the word test
? Or do i need to filter this in several steps?
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
The \b metacharacter matches at the beginning or end of a word.
To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .
\B matches at every position where \b does not. Effectively, \B matches at any position between two word characters as well as at any position between two non-word characters.
Sure, you can use a negative lookahead.
(?!test)[a-zA-Z]{2,4}
I don't know if you'll need it for what you're doing, but note that you may need to use start and end anchors (^
and $
) if you're checking that an entire input matches that pattern. Otherwise, it could match something like ouaeghAEtest
because it will still find four chars somewhere that aren't "test".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With