PHP regex delimiters, / vs. | vs. {} , what are the differences?
In the PHP manual of PCRE, http://us.php.net/m开发者_StackOverflow中文版anual/en/pcre.examples.php, it gives 4 examples of valid patterns:
/<\/\w+>/
|(\d{3})-\d+|Sm
/^(?i)php[34]/
{^\s+(\s+)?$}
Seems that /
, |
or a pair of curly braces can use as delimiters, so is there any difference between them?
No difference, except the closing delimiter cannot appear without escaping.
This is useful when the standard delimiter is used a lot, e.g. instead of
preg_match("/^http:\\/\\/.+/", $str);
you can write
preg_match("[^http://.+]", $str);
to avoid needing to escape the /
.
In fact you can use any non alphanumeric delimiter (excluding whitespaces and backslashes)
"%^[a-z]%"
works as well as
"*^[a-z]*"
as well as
"!^[a-z]!"
精彩评论