简体   繁体   English

正则表达式preg_match_all两个标签和条件之间

[英]Regex preg_match_all between two tags and condition

i'm trying to get the content between two tags with a condition. 我正在尝试使用条件在两个标签之间获取内容。

So here is a example, i want to get all tr's but only the ones with /c in it. 因此,这是一个示例,我想获取所有tr,但仅包含/ c的那些。

example string: 示例字符串:

<tr><td> /a </td></tr>
<tr><td> /b </td></tr>
<tr><td> /c </td></tr>
<tr><td> /d </td></tr>

using what i got so far i get all tr's 使用到目前为止我得到的所有tr

preg_match_all("/<tr.*<\/tr>/", $input_lines, $output_array);

what do i need to do to get this working? 我需要怎么做才能使其正常工作?

thank you 谢谢

preg_match_all("/<td>([^<]*/c[^<]*)<\/td>/", $input_lines, $output_array);

in other words: Whenever the regex will encounter < without having found a /c - it will fail. 换句话说:每当正则表达式遇到<而没有找到/c ,它将失败。

if other html tags could be inside the td, you need to replace [^<]* with .*? 如果其他html标记可能位于td中,则需要将[^<]*替换为.*? .

<td>([^<]*/c[^<]*)<\/td>

正则表达式可视化

Debuggex Demo Debuggex演示

Note: using .*? 注意:使用.*? can lead to unwanted, greedy results see this example: 可能导致不必要的贪婪结果,请参见以下示例:

<td>(.*?/c.*?)<\/td>

正则表达式可视化

Debuggex Demo Debuggex演示

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

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