preg_match() [Function.preg-match]: No ending delimiter ' '

Asked

Viewed 78 times

0

I get some help in putting the correct delimiters below:

if (preg_match('/^[a-zA-Z0-9_-]+[/]{1}[a-zA-Z0-9_-]+$', $bloco)) {
  ....
}

1 answer

3


You placed the delimiter at the beginning of the expression, the same delimiter is missing at the end. Also, you did not escape the slash (/).

'/^[a-zA-Z0-9_-]+\/{1}[a-zA-Z0-9_-]+$/'

Another thing: when you require a single occurrence of a character, it is not necessary to use quantifiers. Then, your expression would look like this:

'/^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+$/'

Finally, one last improvement: you can replace the Character ranges by character classes:

  • a-zA-Z for \w
  • 0-9 for \d

So your expression would look like this:

'/^[\w\d_-]+\/[\w\d_-]+$/'
  • Thank you, but you’ve returned an error : Warning: preg_match() [Function.preg-match]: Unknown Modifier ']'

  • "/" was missing. I changed the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.