且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PCRE 正则表达式非连续重复

更新时间:2023-02-17 22:39:33

有几个问题:

  1. 您的正则表达式模式还将匹配超过 15 个字符的输入.
  2. 由于使用了 \S
  3. ,您的正则表达式还会在中间包含其他不允许使用的字符,例如 @#

您可以通过使用否定前瞻来禁止句点/连字符/下划线的连续出现并从允许任何非空格字符的正则表达式中间删除 \S 来修复它

You can fix it by using a negative lookahead to disallow consecutive occurrence of period/hyphen/underscore and remove \S from middle of regex that allows any non-space character

^[a-zA-Z0-9](?!.*[_.-]{2})[\w.-]{4,13}[a-zA-Z0-9]$

RegEx 演示