preg match compilation error

This is a discussion on preg match compilation error within the PHP General forums, part of the PHP Programming Forums category; Do you realy want to use [ and ] as patern delimiters? My quess is that you want to use it for ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-27-2003
Marek Kilimajer
 
Posts: n/a
Default Re: [PHP] preg match compilation error

Do you realy want to use [ and ] as patern delimiters? My quess is that
you want to use it for defining character class. Then the only character
that must be escaped is -

'/[^a-z0-9,._+()\-]/i'

Luis Lebron wrote:
> I found a reqular expresion that I'm using to check for valid characters in
> file names.
>
> Here's the sample code:
>
> if(preg_match("[^a-zA-Z0-9,._\+\()\-]",$filename))
> {
> //blah, blah, blah
> }
>
> However, when I run the script I get the following error:
> Warning: Compilation failed: unmatched parentheses at offset 17 in
> /home/httpd/sigmarapid/html/test/upload.php on line 137 (the line with the
> preg_match)
>
> I'm new to regular expressions so any help would be greatly appreciated.
>
> Luis R. Lebron
> Sigmatech, Inc
>

Reply With Quote
  #2 (permalink)  
Old 10-27-2003
Luis Lebron
 
Posts: n/a
Default preg match compilation error

I found a reqular expresion that I'm using to check for valid characters in
file names.

Here's the sample code:

if(preg_match("[^a-zA-Z0-9,._\+\()\-]",$filename))
{
//blah, blah, blah
}

However, when I run the script I get the following error:
Warning: Compilation failed: unmatched parentheses at offset 17 in
/home/httpd/sigmarapid/html/test/upload.php on line 137 (the line with the
preg_match)

I'm new to regular expressions so any help would be greatly appreciated.

Luis R. Lebron
Sigmatech, Inc

Reply With Quote
  #3 (permalink)  
Old 10-27-2003
Dvdmandt
 
Posts: n/a
Default Re: preg match compilation error

Start and end with same char, for example with a / or a #... Like
if(preg_match("#[^a-zA-Z0-9,._\+\()\-]#",$filename))


--
// DvDmanDT
MSN: dvdmandt€hotmail.com
Mail: dvdmandt€telia.com
##########################
Please, if you are using windows, you may be infected by Swen. Please go
here to find out more:
http://us.mcafee.com/virusInfo/defau...er&hcName=swen
http://securityresponse.symantec.com...swen.a@mm.html
##########################
"Luis Lebron" <llebron@sigmatech.com> skrev i meddelandet
news:CE5C3E60DCF0D5118C7F00A0C9EBCE23041550@SIGMA2 ...
> I found a reqular expresion that I'm using to check for valid characters

in
> file names.
>
> Here's the sample code:
>
> if(preg_match("[^a-zA-Z0-9,._\+\()\-]",$filename))
> {
> //blah, blah, blah
> }
>
> However, when I run the script I get the following error:
> Warning: Compilation failed: unmatched parentheses at offset 17 in
> /home/httpd/sigmarapid/html/test/upload.php on line 137 (the line with the
> preg_match)
>
> I'm new to regular expressions so any help would be greatly appreciated.
>
> Luis R. Lebron
> Sigmatech, Inc
>

Reply With Quote
  #4 (permalink)  
Old 10-27-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] preg match compilation error

* Thus wrote Luis Lebron (llebron@sigmatech.com):
> I found a reqular expresion that I'm using to check for valid characters in
> file names.
>
> Here's the sample code:
>
> if(preg_match("[^a-zA-Z0-9,._\+\()\-]",$filename))
> {
> //blah, blah, blah
> }


preg_* need a pattern deliminater (most commonly /,#, or !) so it should be:

preg_match("/[^a-zA-Z0-9,._\+\()\-]/",$filename)

Now you can get rid of all the escapes because mosts characters loose
their special meaning inside a character class:

preg_match("/[^a-zA-Z0-9,._+()-]/",$filename)

You can get away without escaping the '-' because it *can't* be
considered a range of characters since it is last (or if its
first).


That should work.


Curt
--
"My PHP key is worn out"

PHP List stats since 1997:
http://zirzow.dyndns.org/html/mlists/
Reply With Quote
  #5 (permalink)  
Old 10-27-2003
Curt Zirzow
 
Posts: n/a
Default Re: [PHP] preg match compilation error

* Thus wrote Marek Kilimajer (kilimajer@webglobe.sk):
> Do you realy want to use [ and ] as patern delimiters? My quess is that
> you want to use it for defining character class. Then the only character
> that must be escaped is -
>
> '/[^a-z0-9,._+()\-]/i'


actually, you don't even need that escape. It has the exception of
being the range character if it is either the first or last
character in the class.


Curt
--
"My PHP key is worn out"

PHP List stats since 1997:
http://zirzow.dyndns.org/html/mlists/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:59 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0