正则表达式匹配除一个单词之外的所有内容
我正在尝试捕获以下模式“除了数据额外的所有内容”
这是搜索字符串:
<li data-extra="star" class = "result">a</li>
<li class = "result">b</li>
<li class = "result">c</li>
<li data-extra="star" class = "result">d</li>
<li class = "result">e</li>
我只想匹配 b、c 和 e(没有数据额外的那些)
我已经做了类似的事情,
<li(?!(data\-extra))class="result"(.*?)>
但是这个不起作用(php preg_match_all 结果为 0)
I'm trying to capture the following pattern "everything except data-extra"
Here is the search string :
<li data-extra="star" class = "result">a</li>
<li class = "result">b</li>
<li class = "result">c</li>
<li data-extra="star" class = "result">d</li>
<li class = "result">e</li>
And I would like to match only b, c and e (those without data-extra)
I've done something like this
<li(?!(data\-extra))class="result"(.*?)>
but this doesn't work (0 result with php preg_match_all)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
xpath_match_all
,如果您还想显式包含该类,请更改XPath 到
$results
变量将包含innerHTML和找到的节点的outerHTML。Use
xpath_match_all
andIf you want to explicitly include the class as well, change the XPath to
The
$results
variable will contain the innerHTML and outerHTML of the found nodes.我认为您错过了正则表达式中的空格。这里有一个 .NET 正则表达式:
I think you missed the whitespaces in your regexp. Here comes one as .NET regexp:
这:
似乎有效 - 尽管我只在这里测试过 - http://regexpal.com/ 但不一定反对 php。
不过,很可能有一种更清洁的方法来做到这一点。
This:
seems to work - though I've only tested it here - http://regexpal.com/ and not necessarily against php.
There may well be a cleaner way to do it though.
这是一种使用正则表达式的方法,尽管 HTML 解析器通常是一个更好的主意:
This is a way that uses regex, although an HTML parser is generally a better idea: