Home  >  Article  >  Backend Development  >  preg_match正则基础题

preg_match正则基础题

WBOY
WBOYOriginal
2016-06-23 14:01:03954browse

<?PHPpreg_match("/^(http:\/\/)?([^\/]+)/i","http://www.php.net/index.html",$arr);print_r($arr);?>


最后结果:http://www.php.net

^(http:\/\/)?可以匹配http://  可以理解

为什么([^\/]+)可以匹配 www.php.net 不解?


回复讨论(解决方案)

[^\/]+表示匹配多个不是‘/’的字符 当然就可以匹配到www.php.net了 因为该字符没有 / 字符

[^\/]+ 表示匹配除了字符”/“的所有字符,当然包括 www.php.net

理解好[^ \/]这个就可以了,表示除“/”这个以外的任何东西,[^ ]这个是特定的,"\"是转义符

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