[thelist] [php] '*' inside regexp (preg_match)

Ken Snyder ksnyder at coremr.com
Tue May 29 16:58:51 CDT 2007


Jonathan Snook wrote:
> On 5/29/07, Ken Snyder <ksnyder at coremr.com> wrote:
>   
>> If you want to allow a dash character, you have to escape it ("\-") or
>> put the dash right before the bracket.  Right now you have
>> space-dash-period (" -.") which will allow any character with an ascii
>> code between space (32) and period (46) among which asterisk (42)
>> happens to fall.  Try this:
>>
>> preg_match("/^[a-zA-Z0-9 \-.(),]+$/", 'abc**def');
>>     
>
> Also, aren't periods normally the "any" character? Wouldn't the period
> have to be escaped as well? or not in ranges?
>   
The only characters that _must_ be escaped within brackets are dashes, 
brackets, and the delimiter character (slash in this case).  You can 
optionally escape other special characters such as periods and parentheses.

I don't know what is "best practice" but I find it more readable to 
escape periods and parentheses in bracketed ranges.

-- Ken



More information about the thelist mailing list