Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Sep 19, 2006 11:25 am
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
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Sep 19, 2006 11:27 am
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...
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Sep 19, 2006 11:40 am
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Sep 19, 2006 11:46 am
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.
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Sep 19, 2006 11:59 am
What are the requirements, completely. Spaces, no spaces, start with something special, length, etc?
Luke
The Ninja Space Mod
Posts: 6424 Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA
Post
by Luke » Tue Sep 19, 2006 12:05 pm
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)
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Sep 19, 2006 2:18 pm
If spaces are fine and it's for form validation then use this:
Code: Select all
$regex = '#^[a-z0-9/,\.\-&\'\s]+$#i';
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Sep 19, 2006 2:22 pm
Very nice. I forget sometimes about the case insensitive 'i ' flag.