regular expression to check number, symbol and alphabet

Posted on 2009-12-27
Last Modified: 2012-05-08
Hi Experts,
any one know how to user regular expression to check whether a string contain number, symbol and alphabet ?

Question by:mkdev2009
    LVL 3

    Expert Comment

    well, you can use this site to help you in REGEX Search and Testing:

    for number check you can use: ^\d*$
    for symbol its a bit more complex - i will need to know the Symbols to help you build that one.
    for alphabet you can use : [-'a-zA-Z]

    hope this helps
    LVL 30

    Expert Comment

    by:Reza Rad
    Regex objNaturalPattern=new Regex("0*[1-9][0-9]*");
    return objNaturalPattern.IsMatch(strNumber);

    this is example for number patterns
    but you can see list of all patterns in this attachements


    Author Comment

    Thanks for reply.
    The regular expression is actually want to validate the password that user create.
    The password that user created must contain alphabet, number and symbols.

    anyway we can do it in one regular expression?

    LVL 13

    Accepted Solution

    to check for at least one symbol,

    to check for all characters must be symbol,

    to check for at least one number
    Note: instead of 0-9 you can use \d for numbers.

    to check for all characters must be number,
    Note: instead of 0-9 you can use \d for numbers.

    to check for at least one alphabet

    to check for all characters must be alphabet
    LVL 13

    Expert Comment

    here is all in single regex. But I don't know what else could be other than symbol, alphabet and numbers???

    Note:: Space is not considered.

    Open in new window

    LVL 3

    Assisted Solution

    I think you need to escape the 1st slash :


    Personally I would not allow so many types of symbols in a password. i'd stick to letters numbers "_" and "-"
    Then the regex would be :


    4-16 also enforces the length to be between 4 and 16 chars.
    LVL 13

    Expert Comment

    >>I think you need to escape the 1st slash :
    You don't need to escape that one. It is the actual regex with proper escape.

    Depending on your programming language, you may need to escape characters. That case you have to escape all back slashes for that regex, not only the first one.
    LVL 3

    Expert Comment

    You do not need to escape "\" when it's inside " [ ] " ?
    LVL 13

    Expert Comment


    Now I got what makes the confusion, you are right from your point of view.

    In perl/php the regex can be written inside the /../ and most of the people use that one.  for example

    so mine one will be as below after inside //

    as my regex is inside the //, that's why i was escaping the forward slash with back slash means \/
    LVL 3

    Expert Comment

    Aha, I am not a perl guy :)
    When I need scripting to mild applications I use TCL.

    And for bigger stuff, C#
    But I have yet to practice regex in C#.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now