PDA

View Full Version : Writing Regular Expressions



Jstewart
06-06-2012, 10:59 AM
Hi there -

I'm currently trying to write some regular expressions for some software we are using and wondering if someone can let me know if I have done the two I need correctly:

One expression is for a username and I have:

"^([a-zA-Z0-9 ]){3,20}$"

This is only meant to allow any letter (lower case or capital), any number between 0-9 and a space and must be between 3 and 20 characters.

For the password I have:

"(?!^[@]*$)(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9@]{8,63})$"

This is only meant to allow @,0-9,a-z and A-Z. This must be between 8 and 63 characters.

Thank in advance!

James

mikebartnz
06-06-2012, 11:17 AM
It would help to know what language you are using. The first part of the first one will need something to separate them.

inphinity
06-06-2012, 11:46 AM
Depending on the language used, the first one looks correct. I would've expected the second to simply be "^[a-zA-Z0-9@]){8,63}$" - again, depending on the language.

Jstewart
06-06-2012, 11:48 AM
To be honest, I have no idea what language this is in. It's just a regular expression that uses the rules to allow users to enter a username and password into our CRM software.

The original codes were:


Const USERNAMEPATTERN = "^([a-zA-Z0-9]){3,20}$"
Const ASSWORDPATTERN = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,63})$"

These codes work perfectly at the moment but I am needing to relax the rules they use slightly to allow spaces in the username and @ in the password, so I changed the code to:

Const USERNAMEPATTERN = "^([a-zA-Z0-9 ]){3,20}$"
Const PASSWORDPATTERN = "(?!^[@]*$)(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9@]{8,63})$"

Regards

James

adslgeek
21-06-2012, 10:36 PM
Infinities second RegEx looks sound to me too, your second looks to be a bit more complex than I think you are describing?