preg_match not working 
March 6th, 2006, 11:05 PM
| | | |
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 | 
March 6th, 2006, 11:15 PM
| | | | 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 | 
March 6th, 2006, 11:15 PM
| | | | 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! | 
March 6th, 2006, 11:25 PM
| | | | 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. | 
March 6th, 2006, 11:25 PM
| | | | 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. :) | 
March 6th, 2006, 11:45 PM
| | | | 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 |  | | | | /bytes/about
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 225,689 network members.
|