正则表达式 - 排除名称的单词列表
我正在尝试制作一个接受以下内容的正则表达式:
- 仅 az、0-9、_ 字符,最小长度为 3
- admin、static、my 和 www 被拒绝。
对于第一部分,我已经设法用 :
^[a-zA-Z0-9\\_]{3,}$
但我不知道如何排除前面列出的单词。
例如,这意味着:
- static 是不允许的(当然),但
- statice 是允许的
- estatic 是允许的
使用此正则表达式:
^(?!static|my|admin|www).*$
不效果不好:它排除了 statice (以及未经授权的单词之后的所有内容)。
您知道哪种正则表达式可以满足我的需要吗?
I'm trying to make a regular expression that accepts this:
- Only a-z, 0-9, _ chars, with a minimum length of 3
- admin, static, my and www are rejected.
For the first part, I already managed to do it with :
^[a-zA-Z0-9\\_]{3,}$
But I don't know how to exclude the words listed previously.
For example, that would mean :
- static is not allowed (of course), but
- statice is allowed
- estatic is allowed
Using this regular expression :
^(?!static|my|admin|www).*$
doesn't work well : it excludes statice (and everything after the unauthorized word).
Do you know which regular expression will fit my need?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样的事情:
这将不允许“static”,但允许“statice”、“statica”等。通过将每个列入黑名单的单词锚定到仅当它们单独存在且没有任何尾随字符时,您才会匹配字符串的末尾。
编辑: codeaddict 建议了一种更简洁的方法来完成基本相同的事情:
Try something like this:
This will disallow "static" but allow "statice", "statica", etc. By anchoring each blacklisted word to the end of the string you will only match them if they are standing alone without any trailing characters.
Edit: codeaddict has suggested a cleaner way to do basically the same thing:
我将回答我的问题,为我的问题提供正确的答案(包含这两个义务的正则表达式),但我将向 Andrew Hare 提供公认的答案,引导我找到正确的方法:)
以下是如何:
这是正则表达式:
或者,正如 Codaddict 提到的,带有单端锚:
希望这对将来有所帮助!
I'll answer my question to give the right answer to my question (a regexp that include both obligations), but I'll give the accepted answer to Andrew Hare that lead me to the correct way :)
Here's how to :
Here is the regexp :
Or, as Codaddict mentionned it, with a single end anchor :
Hope this helps in the future!