简体   繁体   English

正则表达式不超过2个连续数字不超过2个重复字符?

[英]Regex for No more than 2 consecutive numbers No more than 2 repeated characters?

I am looking to create a regex with conditions: 我正在寻找创建带有条件的正则表达式:

  • Minimum length 6 最小长度6
  • At least one number and one letter must be used 必须至少使用一个数字和一个字母
  • No more than 2 consecutive numbers (like 123) 不超过2个连续数字(例如123)
  • No more than 2 repeated characters 不超过2个重复字符

What I am able to achieve 我能达到的目标

/^(?!.*([A-Za-z0-9!@#$&()\\-`.+,/?"])\1{2})(?=.*[a-z])(?=.*\d)[A-Za-z0-9!@#$&()\\-`.+,/?"]+$/

This validates that the string has at least one number and one letter. 这验证了该字符串至少具有一个数字和一个字母。 Instead of consecutive numbers 123, it checks 111. and i am not able to add 4th condition in this. 而不是连续的数字123,它会检查111。并且我无法在其中添加第4个条件。

Any further help will be appreciated. 任何进一步的帮助将不胜感激。 Thanks in advance. 提前致谢。

Try this Regex: 试试这个正则表达式:

^(?=[\D]*\d)(?=[^a-zA-Z]*[a-zA-Z])(?=.{6,})(?!.*(\d)\1{2})(?!.*([a-zA-Z])(?:.*?\2){2,}).*$

Demo 演示

Explanation: 说明:

  • ^ - start of the string ^ -字符串的开头
  • (?=[\\D]*\\d) - positive lookahead - checks for the presence of a digit (?=[\\D]*\\d) -正向超前-检查是否存在数字
  • (?=[^a-zA-Z]*[a-zA-Z]) - positive lookahead - checks for the presence of a letter (?=[^a-zA-Z]*[a-zA-Z]) -正向超前-检查是否存在字母
  • (?=.{6,}) - positive lookahead - checks for the presence of atleast 6 literals (?=.{6,}) -正向超前-检查是否存在至少6个文字
  • (?!.*(\\d)\\1{2}) - Negative lookahead - Checks for the ABSENCE of 3 consecutive digits. (?!.*(\\d)\\1{2}) -负向超前-检查是否连续3位数字缺失。 It will allow 2 consecutive digits though. 它将允许连续2位数字。 If you do not want even 2 consecutive digits, then remove {2} from this part 如果您甚至不希望连续2位数字,请从此部分中删除{2}
  • (?!.*([a-zA-Z])(?:.*?\\2){2,}) - Negative lookahead - validates that no letter should be present more than 2 times in the string (?!.*([a-zA-Z])(?:.*?\\2){2,}) -负向超前-验证字母在字符串中的出现次数不应超过2次
  • .* - capture the string .* -捕获字符串
  • $ - end of the string $ -字符串结尾

OUTPUT: OUTPUT:

jj112233         -Matches as it has atleast one letter, digit. Not more than 2 consecutive digits/letter. Has atleast 6 characters
jkhsfsndbf8uwwe  -matches 
a1234            -does not match as length<6 
nsds312          -matches
111aaa222        -does not match as it has more than 2 consecutive digits and also more than 2 repeated letters 
aa11bbsd         -match
hgshsadh12       -does not match as it has more than 2 `h`
hh8uqweuu        -does not match as it has more than 2 `u`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM