Home  >  Article  >  Backend Development  >  perl - PHP 正则表达式为什么可以用@开头?

perl - PHP 正则表达式为什么可以用@开头?

WBOY
WBOYOriginal
2016-06-06 20:41:09956browse

为什么正则表达式用 @ 开头和结尾,中间的斜杠没有转义,但是却可以使用,未报错?

<code>    if (! preg_match('@^https?://@', $url)) {
        die('HTTP(s) only.');
    }
</code>

回复内容:

为什么正则表达式用 @ 开头和结尾,中间的斜杠没有转义,但是却可以使用,未报错?

<code>    if (! preg_match('@^https?://@', $url)) {
        die('HTTP(s) only.');
    }
</code>

PHP正则的界定符是可以自己定义的,你也可以使用别的,比如#, |之类的

当使用 PCRE 函数的时候,模式需要由分隔符闭合包裹。分隔符可以使任意非字母数字、非反斜线、非空白字符。

经常使用的分隔符是正斜线(/)、hash符号(#) 以及取反符号(~)。
- 分隔符

正则的表带式定界符可以自定义,去看手册吧

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn