Regex Pattern for Not allow all same characters even separated by single special character

huangapple go评论128阅读模式
英文:

Regex Pattern for Not allow all same characters even separated by single special character

问题

  1. 允许包含最少12个字符,最多14个字符的字母数字组合。
  2. 除了一个 & 符号之外,不允许使用特殊字符。
  3. 不允许所有相同的字符,即使它们被 & 符号分隔开。
  4. 字符长度在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 &.

  1. Allow Alpha Numeric characters with a minimum of 12 and a maximum of 14

  2. No special characters are allowed other than & ( only one)

  3. Not Allow all the same characters even separated by &

  4. 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

huangapple
  • 本文由 发表于 2023年4月6日 20:40:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定