PHP正则分隔符,/vs.与{}相比,有什么区别? [英] PHP regex delimiters, / vs. | vs. {} , what are the differences?

查看:71
本文介绍了PHP正则分隔符,/vs.与{}相比,有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PCRE的PHP手册中, http://us.php.net/manual/en/pcre.examples.php ,它提供了4个有效模式的示例:

In the PHP manual of 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+)?$}
  • /<\/\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);

你可以写

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

以避免逃脱/.

这篇关于PHP正则分隔符,/vs.与{}相比,有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆