phpregexpreg-matchereg

PHP: What is wrong with this preg_match that is converted from ereg


I've seen similar questions on here, but I don't think any of them helped with this.

I've inherited a site from someone who used an older version of PHP and specifically the ereg function. Their original reg ex was:

$regex = "[ ]+[0-9]+) ( [a-zA-Z]+)[ ]+Crimson[ ]+([0-9]+)[ ]+[0-9]+[ ]+[0-9]+[ ]+[0-9]+[ ]+([0-9]+)[ ]+[0-9]+";

I've read that you need to start and end with a delimiter, so i've updated it to:

$regex = "/[ ]+[0-9]+) (    [a-zA-Z]+)[ ]+Crimson[ ]+([0-9]+)[ ]+[0-9]+[ ]+[0-9]+[ ]+[0-9]+[ ]+([0-9]+)[ ]+[0-9]+/";

But I am still getting this error:

Warning: preg_match(): Compilation failed: unmatched parentheses at offset 10

I don't see any special characters in the expression, so I am unsure as to what else I need to escape. Any ideas?


Solution

  • You've got ) near the beginning of your regexp, but you didn't open that. If it's literal, add backslash before it.

    /[ ]+[0-9]+)
    

    to

    /[ ]+[0-9]+\)
    

    If not, open it first.