tags:

views:

45

answers:

2

I tried this:

$mtcDatum = preg_match("/[0-9]{2}/[0-9]{2}/[0-9]{4}/", $lnURL);

This returns an error message:

Warning: preg_match() [function.preg-match]: Unknown modifier '['

Why doesn't this work? I'm used to Linux's way of doing regex, does PHP handle regex differently?

+2  A: 

You need a delimiter character around your pattern (this is what separates the pattern from any modifiers). Normally / is used, but as this is part of the string you are trying to match, you can use another character such as #:

$mtcDatum = preg_match("#[0-9]{2}/[0-9]{2}/[0-9]{4}#", $lnURL);
Ben James
Shouldn't it be `"#[0-9]{2}/[0-9]{2}/[0-9]{4}#"`
Amarghosh
And what if I escape the forward slashes `"/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/"`
Amarghosh
Totally right. Note the alternative is to add the slash as the delimiter and then escape all the slashes in your pattern. It's not very readable which is why he suggests choosing an alternate delimiter.
Epsilon Prime
But it seems he forgot to remove `/` from the OP's regex before adding the new delimiter `#`
Amarghosh
I have edited this and removed the leading and trailing `/`. I wasn't sure if these were supposed to be part of the pattern, or the delimiters
Ben James
+5  A: 

PHP syntax is interpreting the "/" character as the end of your pattern. You need to escape the forward slashes:

preg_match("/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/", $lnURL);
davearchie