regex 正则表达式匹配字符串[关闭]

taor4pac  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(93)

**已关闭。**此问题需要debugging details。它目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答这个问题。
上个月关门了。
Improve this question
1.最小长度:8个字符
1.最大长度:18个字符
1.允许的特殊字符:1个点(.)和/或1个下划线(_)
1.特殊字符点和下划线应介于两者之间。特殊字符不能在开头或结尾
1.字母数字:只允许使用数字、字母或数字与字母的任何组合。
我尝试了这个模式,但字符串不匹配:
^[a-z0-9].*[a-z0-9_.][a-z0-9].{8,16}$

u1ehiz5o

u1ehiz5o1#

使用PCRE 2:

(?=^[a-z0-9_.]{8,18}$) (?# Restrict to 8,18 characters by positive lookahead)
^[a-z0-9]+             (?# Must begin with a letter or number )
(?:
  [.][a-z0-9]*_?|      (?# Period then optional underscore)
  _[a-z0-9]*[.]?|      (?# Underscore then optional period)
                       (?# Or Nothing)
)[a-z0-9]+$            (?# Must end with a letter or number)

我有一个想法,如果你添加更多的特殊字符,这个解决方案变得太复杂了。相反,你可以使用更积极的lookaheads。

(?=^.{8,18}$)        (?# Restrict to 8,18 characters by positive lookahead)
(?=^[^.]*[.]?[^.]*$) (?# Restrict to one period by positive lookahead)
(?=^[^_]*[_]?[^_]*$) (?# Restrict to one underscore by positive lookahead)
^[a-z0-9]+           (?# Must begin with a letter or number)
[a-z0-9._]*          (?# All characters allowed in the middle)
[a-z0-9]+$           (?# Must end with a letter or number)

相关问题