0

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?

1
  • Heh, yeah, so based on davearchie & Ben James's answers, the tenth character of your regex is currently the end of your regex, and the function doesn't know what to do with the remaining pile of characters. Commented Nov 24, 2009 at 15:57

2 Answers 2

5

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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);

5 Comments

Shouldn't it be "#[0-9]{2}/[0-9]{2}/[0-9]{4}#"
And what if I escape the forward slashes "/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/"
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.
But it seems he forgot to remove / from the OP's regex before adding the new delimiter #
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.