Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java regex to match all html elements except one special case

I have a string with some markup which looks like this:

The quick brown <a href="www.fox.org">fox</a> jumped over the lazy <a href="entry://id=6000009">dog</a> <img src="dog.png" />.

I'm trying to strip away everything except the anchor elements with "entry://id=" inside. Thus the desired output from the above example would be:

The quick brown fox jumped over the lazy <a href="entry://id=6000009">dog</a>.

Writing this match, the closest I've come so far is:

<.*?>!<a href=\"entry://id=\\d+\">.*?<\\/a>

But I can't figure out why this doesn't work. Any help (apart from the "why don't you use a parser" :) would be greatly appreciated!

like image 722
thomax Avatar asked Feb 23 '26 10:02

thomax


1 Answers

I would really not use regexps for parsing HTML. HTML isn't regular and there are no end of edge cases to trip you up.

Check out JTidy instead.

like image 159
Brian Agnew Avatar answered Feb 24 '26 23:02

Brian Agnew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!