简体   繁体   中英

Regex, exclude filename in html

Html Code

<div class="thx_thanked_post"><a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/star.png"><img src="images/thx/star.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>Just a "quick" overview of what's good and why.<br>

<a title="Click to enlarge" target="_blank" data-fancybox="data-2581" data-type="image" href="images/thx/kek.png"><img src="images/thx/kek.png" alt="Better response on post dqwdqwdqwqdwdqwqwdwdwd" class="thx_thanked_post_img"></a>
<br>
Note: Hyper speed is referenced multiple times here. This is a bug(?) where after reaching speeds over 160 km/h, you continue accelerating like crazy while traveling in a straight line, allowing you to reach speeds of over 500 km/h with a good boost under ideal conditions. Making good use of hyper speed when possible is huge for cutting time, and much of what makes something good is how easily and consistently it works to give hyper speed.<br>

I want to exclude <img> tag that has "stars.png", enclosed by <a> tag.

My regex:

<a [^>]*\>([\t]|[\n])*<img[^>]*^(?!.*star\.png$)[^>]*\>([\t]|[\n])*<\/a>

It doesn't work. This picks up nothing. Correct match should only pickup 2nd <a> tag with <img> that has "images/thx/kek.png" but not "images/thx/star.png".

<a [^>]*>([\\t]|[\\n])*<img(?!.*star\\.png)[^>]*>([\\t]|[\\n])*<\\/a> seems to work.

^ and $ match the start and the end of the line, and they are likely not what you want to match, so I remove them. [^>]* matches all the content in img tag and stops just before the closing > . Lookahead assertion (?!.*star\\.png) after it starts matching from that > and not the content in img tag, so I move it in front of [^>]* .

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