Reputation: 471
I have a HTML content in the form of String
. There are many hyper links in the string. How can I remove only first link in the string? Please guide me.
String html = "abcdef<a href=some dynamic url>link1</a>ghijkl<a href=some url>link2</a>mnopq<a href=some url>link3</a>";
I want to remove the "link1" along with reference url from above string.
Upvotes: 0
Views: 140
Reputation: 8080
You can use a regular expression. Example:
html.replaceFirst("<a[^>]+>[^>]+</a>", "");
Upvotes: 1
Reputation: 2176
I would do something like
String matchATag="<a[^>]*>([^<]+)</a>";
html=html.replaceFirst(matchATag,"");
Upvotes: 2
Reputation: 1682
For html processing i would suggest jsoup (http://jsoup.org/). You can also specify the replacement behaviour in this lib.
Upvotes: 0
Reputation: 597096
You might try to match the link element with regex, but that's a recipe for problems.
You'd better get an HTML parser like NekoHTML, find the first link, and remove it.
Upvotes: 0