开发者

Regular Expression For Alphanumeric String With At Least One Alphabet Or Atleast One Numeric In The String

开发者 https://www.devze.com 2023-02-27 10:28 出处:网络
To test one alphanumeric string we usually use the regular expression 开发者_运维问答\"^[a-zA-Z0-9_]*$\" (or most preferably \"^\\w+$\" for C#). But this regex accepts numeric only strings or alphabet

To test one alphanumeric string we usually use the regular expression 开发者_运维问答"^[a-zA-Z0-9_]*$" (or most preferably "^\w+$" for C#). But this regex accepts numeric only strings or alphabet only strings, like "12345678" or "asdfgth".

I need one regex which will accept only the alphanumeric strings that have at-least one alphabet and one number. That is to say by the regex "ar56ji" will be one of the correct strings, not the previously said strings.

Thanks in advance.


This should do it:

if (Regex.IsMatch(subjectString, @"
    # Match string having one letter and one digit (min).
    \A                        # Anchor to start of string.
      (?=[^0-9]*[0-9])        # at least one number and
      (?=[^A-Za-z]*[A-Za-z])  # at least one letter.
      \w+                     # Match string of alphanums.
    \Z                        # Anchor to end of string.
    ",
    RegexOptions.IgnorePatternWhitespace)) {
    // Successful match
} else {
    // Match attempt failed
} 

EDIT 2012-08-28 Improved efficiency of lookaheads by changing the lazy dot stars to specific greedy char classes.


Try this out:

"^\w*(?=\w*\d)(?=\w*[a-zA-z])\w*$"

There is a good article about it here: http://nilangshah.wordpress.com/2007/06/26/password-validation-via-regular-expression/


This should work:

"^[a-zA-Z0-9_]*([a-zA-Z][0-9]|[0-9][a-zA-Z])[a-zA-Z0-9_]*$"

This will match:

<zero-or-more-stuff>
EITHER <letter-followed-by-digit> OR <digit-followed-by-letter>
<zero-or-more-stuff>

By ensuring you have either a digit followed by letter or a letter followed by digit, you are enforcing the requirement to have at least one digit and at least one letter. Note that I've left out the _ above, because it wasn't clear whether you would accept that as a letter, a digit, or neither.


Try this one ^([a-zA-z]+[0-9][a-zA-Z0-9]*)|([0-9]+[a-zA-z][a-zA-Z0-9]*)$


Simple is better. If you had a hard time writing it originally, you're (or some other poor sap) is going to have a hard time maintaining it or modifying it. (And I think that I see some possible holes in the approaches listed above.)

using System.Text.RegularExpressions;

boolean IsGoodPassword(string pwd){
    int minPwdLen = 8;
    int maxPwdLen = 12;
    boolean allowableChars = false;
    boolean oneLetterOneNumber = false;
    boolean goodLength = false;

    string allowedCharsPattern = "^[a-z0-9]*$";

    //Does it pass the test for containing only allowed chars?
    allowableChars = Regex.IsMatch(pwd, allowedCharsPattern , RegexOptions.IgnoreCase));

    //Does it contain at least one # and one letter?
    oneLetterOneNumber = Regex.IsMatch(pwd, "[0-9]")) && Regex.IsMatch(pwd, "[a-z]", RegularExpressions.IgnoreCase));

    //Does it pass length requirements?
    goodLength = pwd.Length >= minPwdLength && pwd.Length <= maxPwdLength;

    return allowableChars && oneLetterOneNumber && goodLength;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号