Connecting Tech Pros Worldwide Help | Site Map

preg_match not working

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 6th, 2006, 10:05 PM
Andrew Richardson
Guest
 
Posts: n/a
Default preg_match not working

Apologies if this has been asked before - I can't find anything on Google or
Google Groups.

I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed.
For some reason though, the preg_match function is recognised by PHP but it
won't seem to accept any regexes as arguments.

For example:

$result = preg_match("[0-9][0-9]","12")

gives me an error of: Unknown modifier '['

and

$result = preg_match("hello","hello")

gives me an error of: Delimiter must not be alphanumeric or backslash

Am I doing something wrong here?

Thanks in advance

Andrew Richardson

  #2  
Old March 6th, 2006, 10:15 PM
p
Guest
 
Posts: n/a
Default Re: preg_match not working

[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['
>
> and
>
> $result = preg_match("hello","hello")
>
> gives me an error of: Delimiter must not be alphanumeric or backslash[/color]

You still need to wrap the regex in slashes I believe:

$result = preg_match("/[0-9][0-9]/","12")
$result = preg_match("/hello/","hello")

Hope that helps,
Pete
  #3  
Old March 6th, 2006, 10:15 PM
Andrew Richardson
Guest
 
Posts: n/a
Default Re: preg_match not working

p wrote:
[color=blue]
> You still need to wrap the regex in slashes I believe:
>
> $result = preg_match("/[0-9][0-9]/","12")
> $result = preg_match("/hello/","hello")
>
> Hope that helps,
> Pete[/color]

Ah, that would be it. I feel a bit silly now! :)

Thanks!
  #4  
Old March 6th, 2006, 10:25 PM
Michael Winter
Guest
 
Posts: n/a
Default Re: preg_match not working

On 06/03/2006 22:56, Andrew Richardson wrote:
[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['
>
> and
>
> $result = preg_match("hello","hello")
>
> gives me an error of: Delimiter must not be alphanumeric or backslash
>
> Am I doing something wrong here?[/color]

You aren't including delimiters. The first character in a pattern will
be considered to be a pattern delimiter. A closing delimiter marks the
end of the pattern and the start of the flags.

In the first example above, the opening bracket is treated as the
delimiter and the next unescaped closing bracket will end the pattern.

In the second example, there are no delimiters at all.

There are various characters that can be used. Braces and brackets
(round, square, or angle), are considered in pairs. That is, the opening
bracket marks the start of the pattern, and the closing bracket ends it.
Any other non-alphanumeric or backslash character is used twice; the
same character starts and ends the pattern.

A common delimiter is the forward slash:

$result = preg_match('/[0-9][0-9]/', '12');

Hope that helps,
Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
  #5  
Old March 6th, 2006, 10:25 PM
Andrew Richardson
Guest
 
Posts: n/a
Default Re: preg_match not working

Michael Winter wrote:
[color=blue]
> On 06/03/2006 22:56, Andrew Richardson wrote:[/color]

<snip>
[color=blue][color=green]
>> Am I doing something wrong here?[/color]
>
> You aren't including delimiters. The first character in a pattern will
> be considered to be a pattern delimiter. A closing delimiter marks the
> end of the pattern and the start of the flags.[/color]

<snip>
[color=blue]
> Hope that helps,
> Mike[/color]

Thanks Mike, I'd done regexes using preg_match before and forgot about the
delimiters. Now all I have to do is get my regex to work. :)
  #6  
Old March 6th, 2006, 10:45 PM
Toby Inkster
Guest
 
Posts: n/a
Default Re: preg_match not working

Andrew Richardson wrote:
[color=blue]
> $result = preg_match("[0-9][0-9]","12")
>
> gives me an error of: Unknown modifier '['[/color]

$result = preg_match("/[0-9][0-9]/","12");

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.