Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 限制字词中字符的重复_Regex - Fatal编程技术网

Regex 限制字词中字符的重复

Regex 限制字词中字符的重复,regex,Regex,我正在开发一个有角度的web应用程序。表达式用于检查名字和姓氏 允许的字符-字母和“”、“-”。如何限制单词中字符和的重复? 我需要排除像“A-A-A-A”这样的重复。允许: -最多1个“-”字符和1个下划线, -2个“-”字符或2个“_”字符,但必须至少用3个字母分隔 我有一些基本的表达,但没有考虑到这一点: ^(?=.{3,15}$)(?![_-])(?!.*[_-]{2})[a-zA-Z_-]+(?<![_-])$ └─────┬────┘└───┬──┘└─────┬─────┘

我正在开发一个有角度的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
不应通过的示例:

_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
等等


谢谢大家!

我想出了这个笨重的正则表达式。老实说,我认为正则表达式可能不是完成这项任务的最佳方案。但这是一项有趣的任务

^((?=[^-_]*-[^-_]{3,}-[^-_]*$)|(?=[^-_]*-[^-_]{3,}_[^-_]*$)|(?=[^-_]*_[^-_]{3,}-[^-_]*$)|(?=[^-_]*[-_][^-_]*$)|[^-_\n]+$)[\w-]*$
崩溃 我们正在匹配每个
[\w-]*
(任意数量的单词字符,包括下划线和破折号) 有限制 它应该是以下内容之一:

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

(?=[^-\]*-[^-\]{3,}.[^-\]*$)
破折号和下划线之间至少有3个字符

(?=[^-\]*.[^-\]{3,}-[^-\]*$)
用至少3个字符在中间加上下划线和破折号

(?=[^-\]*[-\][^-\]*$)
只要一个破折号或下划线

[^-\n]+$
无破折号或下划线

演示


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

如果:

  • '
    '-'
    ,当它们都存在于字符串中时,必须用除
    '
    '-'
    以外的至少三个字符分隔(当字符串中存在两个
    '
    或两个
    '-'
    时,需要使用这两个字符);及
  • 字符串至少包含两个字符,而不是
    '
    '-'
这两个要求都不是必需的,但它们大大简化了正则表达式

^(?=.{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

请提供许多例子。列出应通过的示例和不应通过的示例。作为文本,直接在此处。请定义您正在使用的正则表达式风格,即命名正则表达式引擎和您正在使用的工具。^另外。你能定义什么是重复吗。“abc-def”也会重复吗?为什么qwerty和qwerty没有通过?您没有指定它,而且根据您的规范,此示例应通过:a-b、ab-c、ab cd、abc de
/
^              # 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