限制单词中的字符顺序重复



我正在开发一个Angular web应用程序。表达式用于检查名字和姓氏。

允许的字符-字母和"_"、"-"。如何限制单词中字符-和_的重复?我需要排除像"A-A-A-A"这样的重复。允许:
-最多1个"-"字符和1个下划线,
-2个"-"字符或2个"_"字符,但它们必须至少用3个字母分隔。

我有一些基本的表达方式,可以很好地工作,但没有考虑到这一点:

^(?=.{3,15}$)(?![_-])(?!.*[_-]{2})[a-zA-Z_-]+(?<![_-])$
└─────┬────┘└───┬──┘└─────┬─────┘└─────┬──┘ └───┬───┘
│         │         │            │        no _ or - at the end
│         │         │         allowed characters
│         │       no __ or _- or -_ or -- inside
│       no _ or - at the beginning
name is 3-15 characters long

要通过的示例:

abcdef-xyz
abc-defxyz
abc-def
abc_def
abc-def-ghi
abc-def_ghi
abc_def_ghi

不应通过的示例:

_qwerty
qwerty_
-qwerty
qwerty-
asd--fff
zxc__cvb
sdf-_cvb
a-b
ab-c
ab-cd
abc-de
a-b-c-d
a_b_c_d
a-b-c_d

等等。

谢谢!

我提出了这个庞大的正则表达式。老实说,我认为regex可能不是这个任务的最佳想法。但做是一项有趣的任务

^((?=[^-_]*-[^-_]{3,}-[^-_]*$)|(?=[^-_]*-[^-_]{3,}_[^-_]*$)|(?=[^-_]*_[^-_]{3,}-[^-_]*$)|(?=[^-_]*[-_][^-_]*$)|[^-_n]+$)[w-]*$

分解我们匹配每个[w-]*(任意数量的单词字符,包括下划线和短划线(有限制它应该是其中之一:

(?=[^-_]*-[^-_]{3,}-[^-_]*$)之间至少有3个字符的两个短划线

之间至少有3个字符的(?=[^-_]*-[^-_]{3,}_[^-_]*$)短划线和下划线

(?=[^-_]*_[^-_]{3,}-[^-_]*$)底核和短划线,之间至少有3个字符

(?=[^-_]*[-_][^-_]*$)只有一个短划线或下划线

[^-_n]+$无短划线或下划线

演示https://regex101.com/r/3MdB5Q/4

正如您所理解的,使用这种方法,它很难在更多的限制(如"最大3个破折号"(上进行扩展。原因是当你需要计数时,regex并不是真正的工具。你可以用技巧来做,但正如你所看到的,它很难扩展,也很难为其他开发人员阅读。因此,最好使用您选择的语言的一些计数功能。

您可以使用以下regex if:

  • '_''-'都存在于字符串中时,它们必须至少由除'_''-'之外的三个字符分隔(当字符串中存在两个'_'或两个'-'时需要(;以及
  • 该字符串包含比CCD_ 13和CCD_

这两个要求都不是必需的,但它们极大地简化了正则表达式。

^(?=.{3,15}$)[^_-]{1,}(?:[_-]?(?:[^_-]{3,}[_-])?)?[^_-]{1,}$

演示

正则表达式可以在自由间距模式中编写,使其具有自文档功能:

/
^              # match beginning of line
(?=.{3,15}$)   # match 3-15 chars followed by end-of-line
[^_-]{1,}      # match 1+ chars other than '_' and '-'
(?:            # begin a non-capture group
[_-]?        # optionally match '_' or '-'
(?:          # begin a non-capture group
[^_-]{3,}  # match 3+ chars other than '_' and '-'
[_-]       # match '_' or '-'
)?           # end non-capture group
)?             # end non-capture group
[^_-]{1,}      # match 1+ chars other than '_' and '-'    
$              # match end of line
/x             # free-spacing regex definition mode

最新更新