简体   繁体   中英

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?

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.

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

Did the trick for me. Thanks for your answers guys

我认为下面的正则表达式应该可以解决问题

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM