Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

RegEx No more than 2 identical consecutive characters and a-Z and 0-9

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

http://regexr.com?34vo9

The regular expression:

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

The first lookahead ((?=.{8,20}$)) checks the length of your string. The second portion does your double character and validity checking by:

(
  ([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.

Okay. I see you've added some additional requirements. My basic forumla still works, but we have to give you more of a step by step approach. SO:

^...$

Your whole regular expression will be dropped into start and end characters, for obvious reasons.

(?=.{n,m}$)

Length checking. Put this at the beginning of your regular expression with n as your minimum length and m as your maximum length.

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

Required characters. Place this at the beginning of your regular expression with REQ as your required character to require N to M of your character. YOu may drop the (?: ..){n,m} to require just one of that character.

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

The rest of your expression. Replace VALID with your valid Characters. So, your Password Regex is:

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

'Splained:

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

http://regexr.com?34vol Here's the new one in action.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...