简体   繁体   中英

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.

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

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.

if other html tags could be inside the td, you need to replace [^<]* with .*? .

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

正则表达式可视化

Debuggex Demo

Note: using .*? can lead to unwanted, greedy results see this example:

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

正则表达式可视化

Debuggex Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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