Regex: a matching pattern, but not a specific word.

Is it possible to write a regular expression that matches for [a-zA-Z]{2,4} but not for the word test ? Or do I need to filter this out in a few steps?

+4
source share
2 answers

Of course, you can use negative viewing .

 (?!test)[a-zA-Z]{2,4} 

I don’t know if you need this for what you are doing, but note that you may need to use start and end bindings ( ^ and $ ) if you check that the entire input matches this pattern. Otherwise, it may correspond to something like ouaeghAEtest , because it will still find four characters that are not “tests”.

+12
source
 [A-Za-su-z][A-Za-df-z]{0,1}[A-Za-rt-z]{0,1}[A-Za-su-z]{0,1} 

just an idea, don't use real code to try

-1
source

Source: https://habr.com/ru/post/1434001/


All Articles