PCRE 正则表达式非连续重复 [英] PCRE Regex non-consecutive repeating

查看:52
本文介绍了PCRE 正则表达式非连续重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试最少 6 个字符,最多 15 个字符.第一个必须是字母数字(无特殊),接下来(最多)13 个是字母数字,并且可以包含非连续(并且一次只能包含以下一个)下划线或句点或连字符,然后最后一个字符必须是字母数字.

I'm trying to have a 6 character minimum, up to 15 chars total. First has to be alphanumeric (no special), next (up to) 13 to be alphanumeric and can include NON CONSECUTIVE (and only one of the following at a time) underscore OR period OR hyphen, then last character has to be alphanumeric.

好的例子:A_3.hj_3J

example of okay: A_3.hj_3J

例子不好:F__3d66.K

example not okay: F__3d66.K

例子不好:6-_sd.6h9

example not okay: 6-_sd.6h9

这是我到目前为止所拥有的,我觉得它很接近但很烦人.我做错了什么?

This is what I have so far, I feel like it's close but annoyingly off. What am I doing wrong?

^[a-zA-Z0-9]{1}([_.-]?[a-zA-Z0-9])\S{4,13}[a-zA-Z0-9]{1}$

推荐答案

有几个问题:

  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 演示

这篇关于PCRE 正则表达式非连续重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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