reox
reox

Reputation: 5217

Regex: match pattern but not certain word

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?

Upvotes: 10

Views: 3968

Answers (2)

倪玉哲
倪玉哲

Reputation: 27

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

just a idea, haven't use real code to try

Upvotes: -1

Wiseguy
Wiseguy

Reputation: 20873

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".

Upvotes: 17

Related Questions