Search code examples
phpregexvalidationdelimiter

Regex pattern starting with an opening square brace and ending with a closing square brace gives a Warning


I'm looking for a regex that will match against: X_X

Where X is always a number great than zero and there must be an underscore character in between the two numbers.

EDIT: When I run

$pattern = "[1-9]_[1-9]";
if (preg_match($pattern, $key)) 
    return TRUE;

I get a Warning:

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


Solution

  • The following should do what you want:

    preg_match("/[1-9]\d*_[1-9]\d*/", $key)
    

    Note that this will work for numbers with more than one digit as well, if X in your example is only a single digit number then you can remove the \d*.