maverickosama92 maverickosama92 - 6 months ago 28
Java Question

Remove this specific string from string

I have following code to remove index.php occurrences from my string:

final Pattern p = Pattern.compile("\\\"index\\.php\\?[^\"]*[;?]+id=([0-9]+)[^\"]*\\\"");
String str = "1 serving Well-Seasoned Oven Roasted Pork Tenderloin<a href=\"index.php?option=com_recipe&amp;Itemid=101&amp;r=794\"> (recipe)</a><br />1 serving Parsley Garlic Potatoes <a href=\"index.php?option=com_recipe&amp;Itemid=101&amp;r=668\">(recipe)</a><br />\n</div>";

Matcher m = p.matcher(str);
while(m.find()) {
try {
String match = m.group();
str = str.replaceAll(match, "abc.html");
p.matcher(str);
} catch(Exception e) {
e.printStackTrace();
}
}

System.out.printf("final:" + str);


First I want to get this "index.php?option=com_recipe&Itemid=101" and then get item id value.

Thanks

Answer

I used jsoup library and fixed it, following is my code:

Document doc = Jsoup.parse(data);
Elements elements = doc.select("a");
for(Element element: elements){
     String href = element.attr("href");
     data = data.replace(href, "abc.html");
}