maverickosama92 maverickosama92 - 2 years ago 112
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 =;
str = str.replaceAll(match, "abc.html");
} catch(Exception e) {

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

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


Answer Source

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

Document doc = Jsoup.parse(data);
Elements elements ="a");
for(Element element: elements){
     String href = element.attr("href");
     data = data.replace(href, "abc.html");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download