英文:
Regex Pattern for Not allow all same characters even separated by single special character
问题
- 允许包含最少12个字符,最多14个字符的字母数字组合。
- 除了一个 & 符号之外,不允许使用特殊字符。
- 不允许所有相同的字符,即使它们被 & 符号分隔开。
- 字符长度在12到14之间,包括 & 符号。
^(\d)(?!+$)(?=.{11,13}$)[0-9]*(?:[&][0-9]+)?$
以上正则表达式在除了情况 "1111111&11111" 外均有效。
以下正则表达式也符合要求,不允许相同字符,但允许 & 符号出现在字符串开头或结尾,并且允许大写字母和数字的混合。
^(?!&?(\d)(?:&?)+$)(?=.{12,14}$)\d*(?:&\d*)?$
不允许字符串开头或结尾出现"&"
允许大写字母字符与数字的混合。
示例:
- ABC123567GHDG4 允许
- ABC1235&67GHD 允许
- ABCDEFGHIJKLR 允许
- 1254789563254 允许
- 125478&563254 允许
- 125478556325& 不允许,因为以 & 结尾
- &125478556325 不允许,因为以 & 开头
- ABCDEFGHIJKL& 不允许,因为以 & 结尾
- &ABCDEFGHIJKL 不允许,因为以 & 开头
英文:
Trying to create the regex for the below conditions, I have tried the one regex it's working as expected other than one condition as it does not allow duplicates even separated by particular Char &.
-
Allow Alpha Numeric characters with a minimum of 12 and a maximum of 14
-
No special characters are allowed other than & ( only one)
-
Not Allow all the same characters even separated by &
-
Length is same between 12 to 14 even including the &
ex: 111111111111 not allowed as all are the same characters
1111111&11111 not allowed as all are the same characters even & is there
111111111112 allowed
1111111&11112 allowed
ABC123567GHDG4 allowed
^(\d)(?!+$)(?=.{11,13}$)[0-9]*(?:[&][0-9]+)?$
The above regex is working other than the case 1111111&11111
tried the below regex as well, it works the condition as it's not allowing the same characters
But it allows the & beginning and end of the string and not allowed Alphabets
^(?!&?(\d)(?:&?)+$)(?=.{12,14}$)\d*(?:&\d*)?$
We do not allow the "&"
at the beginning and end of the string
And it needs to allow the Captial Alpha character
along with numerics
.
ABC123567GHDG4 allowed
ABC1235&67GHD allowed
ABCDEFGHIJKLR allowed
1254789563254 allowed
125478&563254 allowed
125478556325& not allowed as ends with &
&125478556325 not allowed as starts with &
ABCDEFGHIJKL& not allowed as ends with &
&ABCDEFGHIJKL not allowed as start with &
答案1
得分: 2
^(?=.{12,14}$)(?!([a-zA-Z\d])(?:&?)+$)[a-zA-Z\d]+(?:&[a-zA-Z\d]+)?$
详情:
^
- 字符串的开始(?=.{12,14}$)
- 字符串的长度必须在12到14个字符之间(?!([a-zA-Z\d])(?:&?\1)+$)
- 字符串不能以相同的字母数字字符结尾,可以在任何位置用&
分隔[a-zA-Z\d]+(?:&[a-zA-Z\d]+)?
- 一个或多个字母数字字符,后跟一个可选的序列,包括一个&
字符,后跟一个或多个字母数字字符$
- 字符串的结尾。
英文:
You can use
^(?=.{12,14}$)(?!([a-zA-Z\d])(?:&?)+$)[a-zA-Z\d]+(?:&[a-zA-Z\d]+)?$
See the regex demo
Details:
^
- start of string(?=.{12,14}$)
- the length of the string must be between 12 and 14 chars(?!([a-zA-Z\d])(?:&?\1)+$)
- the string cannot have identical alphanumeric chars eventually separated with a&
char at any position[a-zA-Z\d]+(?:&[a-zA-Z\d]+)?
- one or more alphanumeric chars followed with an optional sequence of a&
char followed with one or more alphanumeric chars$
- end of string.
答案2
得分: 1
这是修改过的模式:
/^([A-Z0-9])(?!(|&)+$)(?=.{11,13}$)[A-Z0-9]*(?:[&][A-Z0-9]+)?$/gm
我只更新了负向预查部分为 (?!(\1|&)+$)
,不允许第一个字母或 &
重复。
在这里查看演示。
英文:
You could try this pattern:
/^([A-Z0-9])(?!(|&)+$)(?=.{11,13}$)[A-Z0-9]*(?:[&][A-Z0-9]+)?$/gm
It's modified version of your pattern, I've only updated the negative look-ahead part to (?!(\1|&)+$)
(not allow repeated of first letter or '&' letter).
See demo here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论