正则表达式-排除名称的单词列表 [英] Regular Expression - Exclude list of words for a name

查看:110
本文介绍了正则表达式-排除名称的单词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个接受此内容的正则表达式:

I'm trying to make a regular expression that accepts this:

  • 仅a-z,0-9,_个字符,最小长度为3
  • 管理员,静态,我的和www被拒绝.

对于第一部分,我已经设法做到了:

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.

例如,这意味着:

    (当然)不允许
  • 静态,但是
  • 允许
  • 静态
  • 允许
  • 静态
  • static is not allowed (of course), but
  • statice is allowed
  • estatic is allowed

使用此正则表达式:

^(?!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?

推荐答案

尝试如下操作:

^(?!static$|my$|admin$|www$).*$

这将不允许使用"static",但允许使用"statice","statica"等.通过将每个列入黑名单的单词锚定到字符串的末尾,仅当它们单独站立且没有任何尾随字符时,您才匹配它们.

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.

codeaddict 提出了一种更干净的方法来完成基本相同的操作:

codeaddict has suggested a cleaner way to do basically the same thing:

^(?!(?:static|my|admin|www)$).*$

这篇关于正则表达式-排除名称的单词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆