Page 1 of 1

need some help with matching alpha-num with some punctuation

Posted: Tue Sep 19, 2006 11:25 am
by Luke
I am really terrible at regex. I have an O'Reilly regex book on my wishlist, but for now, can somebody help me put together a regular expression to match 1 or more of alpha-numeric characters and allow also these characters: /,.-&'

here is what I have... and it doesn't work

Code: Select all

'/^[a-zA-Z0-9/\,\.-&\']+$/'

Posted: Tue Sep 19, 2006 11:27 am
by RobertGonzalez
RegExpLib

EDIT | Are there any other contraints? Like having to start with a letter, not with a number or special char, etc.

EDIT 2 | Try this one...

Code: Select all

[A-Z0-9a-z\'\%\\\\\&]

Posted: Tue Sep 19, 2006 11:40 am
by Oren

Code: Select all

$regex = '#[a-z0-9/,\.\-&\']+#i';

Posted: Tue Sep 19, 2006 11:46 am
by Luke
Oren wrote:

Code: Select all

$regex = '#[a-z0-9/,\.\-&\']+#i';
I tried this one above, but it allows the following to get through...
Luke's the !@#$^ Coolest guy ever!

I imagine it's because it's matching the first letter and returning true... I should clarify... it's for a form field... so it has to match the entire string.

Posted: Tue Sep 19, 2006 11:59 am
by RobertGonzalez
What are the requirements, completely. Spaces, no spaces, start with something special, length, etc?

Posted: Tue Sep 19, 2006 12:05 pm
by Luke
Everah wrote:What are the requirements, completely. Spaces, no spaces, start with something special, length, etc?
spaces are ok... doesn't need to start with anything special... 45 characters (although length is not strictly necessary)

Posted: Tue Sep 19, 2006 12:53 pm
by RobertGonzalez

Code: Select all

$pattern = '#^[a-zA-Z0-9\s.\-\&\/\,\\\']+$#';

Posted: Tue Sep 19, 2006 2:18 pm
by Oren
If spaces are fine and it's for form validation then use this:

Code: Select all

$regex = '#^[a-z0-9/,\.\-&\'\s]+$#i';

Posted: Tue Sep 19, 2006 2:22 pm
by RobertGonzalez
Very nice. I forget sometimes about the case insensitive 'i' flag.