PHP regex delimiters, / vs. | vs. {} , what are the differences?

后端 未结 2 612
孤城傲影
孤城傲影 2020-12-19 08:59

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

  • /<\\/\\w+>/
相关标签:
2条回答
  • 2020-12-19 09:31

    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]!"
    
    0 讨论(0)
  • 2020-12-19 09:43

    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 /.

    0 讨论(0)
提交回复
热议问题