我有以下代码从我的字符串中删除index.php出现次数:
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&Itemid=101&r=794\"> (recipe)</a><br />1 serving Parsley Garlic Potatoes <a href=\"index.php?option=com_recipe&Itemid=101&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);
首先,我想获得“index.php?option = com_recipe&amp; Itemid = 101”,然后获取商品ID值。
由于
答案 0 :(得分:0)
[所做的更改]如何关注:
String str = "1 serving Well-Seasoned Oven Roasted Pork Tenderloin<a href=\"index.php?option=com_recipe&Itemid=101&r=794\"> (recipe)</a><br />1 serving Parsley Garlic Potatoes <a href=\"index.php?option=com_recipe&Itemid=101&r=668\">(recipe)</a><br />\n</div>";
str = str.replaceAll("\\<a.*?\\</a>", "<a href=\\\\\"abc.html>(recipe)</a>");
System.out.printf("final:" + str);
答案 1 :(得分:0)
这应该替换所有出现的事件:
str = str.replaceAll("index.php", "abc.html");
为什么在这里使用模式?
答案 2 :(得分:0)
试试这个:
String str = "1 serving Well-Seasoned Oven Roasted Pork Tenderloin<a href=\"index.php?option=com_recipe&Itemid=101&r=794\"> (recipe)</a><br />1 serving Parsley Garlic Potatoes <a href=\"index.php?option=com_recipe&Itemid=101&r=668\">(recipe)</a><br />\n</div>";
str = str.replaceAll("<a href=\"index.php.*?>", "<a href=\"abc.html\">");
System.out.printf("final:" + str);
输出:
final:1 serving Well-Seasoned Oven Roasted Pork Tenderloin<a href="abc.html"> (recipe)</a><br />1 serving Parsley Garlic Potatoes <a href="abc.html">(recipe)</a><br />
答案 3 :(得分:0)
我使用了jsoup库并修复了它,以下是我的代码:
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");
}