繁体   English   中英

REGEX无法正常运作,preg_match

[英]REGEX not functioning properly, preg_match

这个REGEX无法正常运作...

/<ol class=\"references\">\/(.*?)<\/ol>/s

我将它与PHP中的preg_match结合使用。

preg_match_all("/<ol class=\"references\">\/(.*?)<\/ol>/s", $file, $mats);

当我将其放在http://regex101.com/中时 ,似乎出现的问题是它希望将要匹配/解析的字符串设置为<ol class="references">/text here</ol>

它指出: \\/ matches the character / literally

但是,我希望REGEX和PHP的代码片段解析<ol class="references">text here</ol>

正确的\\/匹配文字斜杠,因此您的模式无法匹配:

<ol class="references">text here</ol>

由于表达式需要文字/在第一个> 只需删除它,它应该可以按要求工作:

<ol class=\"references\">(.*?)<\/ol>

如果元素的内部文本中偶尔有不需要的斜杠( / ),则可以进行量化匹配- ? -像这样:

<ol class=\"references\">\/?(.*?)<\/ol>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM