【问题标题】:RegEx No more than 2 identical consecutive characters and a-Z and 0-9RegEx 不超过 2 个相同的连续字符以及 a-Z 和 0-9
【发布时间】:2013-05-23 15:13:23
【问题描述】:

编辑:感谢您的建议,让我的问题更清楚:)

Match 正在寻找 3 个连续的字符:

Regex Match =AaA653219
Regex Match = AA5556219

代码是 ASP.NET 4.0。这是整个函数:

public ValidationResult ApplyValidationRules()
{
    ValidationResult result = new ValidationResult();
    Regex regEx = new Regex(@"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");

    bool valid = regEx.IsMatch(_Password);
    if (!valid)
        result.Errors.Add("Passwords must be 8-20 characters in length, contain at least one alpha character and one numeric character");

    return result;
}

我已经尝试了 3 个多小时来完成这项工作,但没有运气参考以下内容 =/

How can I find repeated characters with a regex in Java?

.net Regex for more than 2 consecutive letters

我已经开始使用 8-20 个字符 a-Z 0-9

^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$
As Regex regEx = new Regex(@"^(?=.*\d)(?=.*[a-zA-Z]).{8,20}$");

我尝试添加以下变体,但没有成功:

/(.)\1{9,}/
.*([0-9A-Za-z])\\1+.*
((\\w)\\2+)+". 

任何帮助将不胜感激!

【问题讨论】:

  • 发布示例输入和预期的匹配项。
  • 语言是什么? VB.NET?
  • 这必须是一个正则表达式吗?听起来您只需要检查与 [a-zA-Z0-9]{8,20} 匹配的字符串,并且还 not 匹配 .*(.)\1\1.*

标签: regex


【解决方案1】:

http://regexr.com?34vo9

正则表达式:

^(?=.{8,20}$)(([a-z0-9])\2?(?!\2))+$

第一个前瞻 ((?=.{8,20}$)) 检查字符串的长度。第二部分通过以下方式检查您的双重字符和有效性:

(
  ([a-z0-9])      Matching a character and storing it in a back reference.
  \2?             Optionally match one more EXACT COPY of that character.
  (?!\2)          Make sure the upcoming character is NOT the same character.
)+                Do this ad nauseum.
$                 End of string.

好的。我看到你添加了一些额外的要求。我的基本论坛仍然有效,但我们必须为您提供更多一步一步的方法。所以:

^...$

出于显而易见的原因,您的整个正则表达式将被放入开始和结束字符中。

(?=.{n,m}$)

长度检查。将其放在正则表达式的开头,其中 n 为最小长度,m 为最大长度。

(?=(?:[^REQ]*[REQ]){n,m})

必填字符。把它放在你的正则表达式的开头,用 REQ 作为你需要的字符来要求你的字符的 N 到 M。您可以删除 (?: ..){n,m} 以只需要其中一个字符。

(?:([VALID])\1?(?!\1))+

你表达的其余部分。将 VALID 替换为您的有效字符。所以,你的密码正则表达式是:

^(?=.{8,20}$)(?=[^A-Za-z]*[A-Za-z])(?=[^0-9]*[0-9])(?:([\w\d*?!:;])\1?(?!\1))+$

'解释:

^
  (?=.{8,20}$)                 8 to 20 characters
  (?=[^A-Za-z]*[A-Za-z])       At least one Alpha
  (?=[^0-9]*[0-9])             At least one Numeric
  (?:([\w\d*?!:;])\1?(?!\1))+  Valid Characters, not repeated thrice.
$

http://regexr.com?34vol 这是新的行动。

【讨论】:

  • @TacRedline 不客气。我已经添加了烘焙你自己的验证器 RegEx 的说明,它应该允许你做所有额外的事情。不过,这些东西很快就会变得非常难以阅读——你可能想要部分地编译它以获得更好的自我文档,或者提供很多评论。如果它回答了您的问题,请随时将此答案标记为“已接受”。
  • @FrankieTheKneeMan 我尝试将正则表达式与谓词一起用于连续字符,但是当有两个重复字符时,我得到“真”,但如果三个或两个以上连续字符我得到假。这是我的代码,让 ContinuousRegEx = "(?:([\\w\\d*?!:;])\\1?(?!\\1))+$" let pred = NSPredicate(format: "自匹配 %@ ", continuousRegEx) debugPrint(" isConNumber - (pred.evaluate(with: password))")
【解决方案2】:

收紧匹配标准,因为它太宽泛了;例如,“not A-Za-z”匹配的内容比预期的要多得多。之前的正则表达式匹配字符串“ThiIsNot”。在大多数情况下,密码只会包含字母数字和标点符号,所以我限制了范围,这使得所有匹配更加准确。用于人类可读性的字符类。添加和排除列表,区分大小写字母。

^(?=.{8,20}$)(?!(?:.*[01IiLlOo]))(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1})(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1})(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1})(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1})(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$

细分:

^(?=.{8,20}$) - Positive lookahead that the string is between 8 and 20 chars
(?!(?:.*[01IiLlOo])) - Negative lookahead for any blacklisted chars
(?=(?:[\[[:digit:]\]\[[:punct:]\]]*[\[[:alpha:]\]]){2}) - Verify that at least 2 alpha chars exist
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:upper:]\]]*[\[[:lower:]\]]){1}) - Verify that at least 1 lowercase alpha exists
(?=(?:[\[[:digit:]\]\[[:punct:]\]\[[:lower:]\]]*[\[[:upper:]\]]){1}) - Verify that at least 1 uppercase alpha exists
(?=(?:[\[[:alpha:]\]\[[:punct:]\]]*[\[[:digit:]\]]){1}) - Verify that at least 1 digit exists
(?=(?:[\[[:alnum:]\]]*[\[[:punct:]\]]){1}) - Verify that at least 1 special/punctuation char exists
(?:([\[[:alnum:]\]\[[:punct:]\]])\1?(?!\1))+$ - Verify that no char is repeated more than twice in a row

【讨论】:

    猜你喜欢
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多