the regex does not allow comma for me

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

the regex does not allow comma for me

Post by raghavan20 »

I am trying to allow a few characters in a pattern match and I want to allow comma as well. I am trying to escape comma since it is a special character but the regex engine does not seem to accept.

Code: Select all

preg_match("#[^a-zA-Z0-9\s\.-\,]#", $_POST['city'])


error: Warning: preg_match() [function.preg-match]: Compilation failed: range out of order in character class at offset 17
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A comma isn't a metacharacter last I checked. The complaint is coming from your usage of the minus sign, which is a metacharacter in a character class. If you want to allow a minus to appear in the match, escape it. Otherwise, you'll need to swap the comma and period to make them in ordinal ascension.
Post Reply