PHP Regular Expression Limitations, / vs. | vs. {} what are the differences?

In the PHP manual for PCRE http://us.php.net/manual/en/pcre.examples.php, it gives 4 examples of valid patterns:

  • /<\/\w+>/
  • |(\d{3})-\d+|Sm
  • /^(?i)php[34]/
  • {^\s+(\s+)?$}

It seems that /, |or a pair of curly braces can be used as delimiters, so is there any difference between them?

+3
source share
2 answers

There is no difference except that the closing delimiter cannot appear without escaping.

This is useful when the standard delimiter is used a lot, for example. instead

preg_match("/^http:\\/\\/.+/", $str);

You can write

preg_match("[^http://.+]", $str);

to avoid the need to avoid /.

+3
source

- ( )

"%^[a-z]%"

,

"*^[a-z]*"

"!^[a-z]!"
+3

Source: https://habr.com/ru/post/1746770/


All Articles