Having for example such a string:
<a href="LINK_1" class="am"> Some Text</a>.. ANYTHING ..<a href="LINK_2" class="am"> Some Text</a><a href="SEARCHED_HREF_TO_EXTRACT" class="am"> SEARCHED_TEXT</a>..
I need to extract from a HTML link a 'href' attribute value, from a link which contains some searched word like 'SEARCHED_TEXT' in example. Could you please advice, how to do it correctly? Would not ask if not sent much time already =)
I went till this, but unhopefully it works incorrectly..
String str = "<a href=\"LINK_1\" class=\"am\"> Some Text</a>.. ANYTHING ..<a href=\"LINK_2\" class=\"am\"> Some Text</a><a href=\"SEARCHED_HREF_TO_EXTRACT\" class=\"am\"> SEARCHED_TEXT</a>";
Pattern pattern = Pattern.compile("<a.*?href=\"(.*?)\".*SEARCHED_TEXT</a>");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(0)); // matched whole string
System.out.println(matcher.group(1)); // should be SEARCHED_HREF_TO_EXTRAC
I see that I need some negotation after href="(.*?)" to accept all symbols except
</a>
to find correct HREF, but can't make it work :(