Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1034
  • Last Modified:

regular expression to check number, symbol and alphabet

Hi Experts,
any one know how to user regular expression to check whether a string contain number, symbol and alphabet ?

Thanks.
0
mkdev2009
Asked:
mkdev2009
2 Solutions
 
roeibCommented:
well, you can use this site to help you in REGEX Search and Testing:

regexlib.com

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
0
 
Reza RadCommented:
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

http://www.addedbytes.com/cheat-sheets/download/regular-expressions-cheat-sheet-v2.pdf

0
 
mkdev2009Author Commented:
HI,
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?

Thanks
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
marchentCommented:
to check for at least one symbol,
[!-\/:-@\[-`\{-~]

to check for all characters must be symbol,
^[!-\/:-@\[-`\{-~]+$

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

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

to check for at least one alphabet
[a-zA-Z]

to check for all characters must be alphabet
^[a-zA-Z]+$
0
 
marchentCommented:
here is all in single regex. But I don't know what else could be other than symbol, alphabet and numbers???

^[!-\/:-@\[-`\{-~a-zA-Z0-9]+$

Note:: Space is not considered.

Open in new window

0
 
eitamaCommented:
I think you need to escape the 1st slash :

^[!-\\/:-@\[-`\{-~a-zA-Z0-9]+$

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 :

^[a-zA-Z\d-_]{4-16}$

4-16 also enforces the length to be between 4 and 16 chars.
0
 
marchentCommented:
@eitama
>>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.
0
 
eitamaCommented:
You do not need to escape "\" when it's inside " [ ] " ?
0
 
marchentCommented:
@eitama

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
/YOUR REGEX/

so mine one will be as below after inside //
/^[!-\/:-@\[-`\{-~a-zA-Z0-9]+$/

as my regex is inside the //, that's why i was escaping the forward slash with back slash means \/
0
 
eitamaCommented:
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#.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now