ManjushaDC
ManjushaDC

Reputation: 190

RegEx to exclude particular words

I am working on RegEx that will match a string that contains only uppercase letters and digits but not exact words ABC or XYZ or LMN.

^(?!ABC|XYZ|LMN)([A-Z0-9]+)$

This works for most of the cases but fails for input ABC12, XYZ8 etc.

How can I improve this to match the requirement.

I've referred RegEx to exclude a specific string constant but couldn't tackle this one.

Upvotes: 0

Views: 436

Answers (1)

Matt Miguel
Matt Miguel

Reputation: 1375

If you are trying to match the whole string or line using ^ and $, then you can put the $ inside your negative lookahead. This will allow ABC12 to match.

^(?!(ABC|XYZ|LMN)$)([A-Z0-9]+)$

Upvotes: 1

Related Questions