19

Edit: Thanks for the advice to make my question clearer :)

The Match is looking for 3 consecutive characters:

Regex Match =AaA653219
Regex Match = AA5556219

The code is ASP.NET 4.0. Here is the whole function:

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;
}

I've tried for over 3 hours to make this work, referencing the below with no luck =/

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

.net Regex for more than 2 consecutive letters

I have started with this for 8-20 characters a-Z 0-9 :

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

I've tried adding variations of the below with no luck:

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

Any help would be much appreciated!

4

2 回答 2

41

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 这是新的行动。

于 2013-05-23T15:21:15.077 回答
1

收紧匹配标准,因为它太宽泛了;例如,“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
于 2021-08-01T19:18:32.297 回答