Spidi
Spidi

Reputation: 109

Regular expression to exclude specific words but allow certain patterns

I am generating a regular expression that can contain any letters or numbers or an underscore [a-zA-Z0-9_] but not contain words that exactly match log, login and lastly test.

Can anybody help me with this?

Upvotes: 1

Views: 86

Answers (3)

Spidi
Spidi

Reputation: 109

 ^(?!(^test$)|(^log$)|(^login$))([A-Za-z0-9_-/]+)$ 

Did the trick for me. Thanks for your answers guys

Upvotes: 1

Minh
Minh

Reputation: 424

I think the below regular expression should do the trick

^((?!log|login|test)[a-zA-Z0-9_])*$

Upvotes: 0

anubhava
anubhava

Reputation: 785156

You can use this negative lookahead regex:

\b(?!log(?:in)?|test)\w+

RegEx Demo

(?!log(?:in)?|test) is negative lookahead, that will fail the match if any given words log,login,test are present.

Upvotes: 3

Related Questions