Java 从字符串中删除此特定字符串
我有以下代码从字符串中删除index.php实例:Java 从字符串中删除此特定字符串,java,string,replace,Java,String,Replace,我有以下代码从字符串中删除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&a
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);
final Pattern p=Pattern.compile(“\\\”索引\\.php\\?[^\”]*[;?]+id=([0-9]+)[^\“]*\\”);
String str=“1份调味充分的烤箱烤猪里脊
1份欧芹蒜土豆
\n”;
匹配器m=p.Matcher(str);
while(m.find()){
试一试{
字符串匹配=m.group();
str=str.replaceAll(匹配“abc.html”);
p、 匹配器(str);
}捕获(例外e){
e、 printStackTrace();
}
}
系统输出打印(“最终:+str”);
首先我想得到这个“index.php?option=com\u recipe&Itemid=101”,然后得到Itemid值
谢谢[更改]以下内容如何:
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);
String str=“1份调味良好的烤箱烤猪里脊
1份欧芹大蒜土豆
\n”;
str=str.replaceAll(“\\”);
系统输出打印(“最终:+str”);
这应该可以替换所有发生的事件:
str = str.replaceAll("index.php", "abc.html");
为什么在这里使用模式?试试这个:
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);
String str=“1份调味良好的烤箱烤猪里脊
1份欧芹大蒜土豆
\n”;
str=str.replaceAll(“
1份欧芹大蒜土豆
我使用了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");
}
我不明白你的问题。如果你只是想用“abc.html”替换“index.php”,那么一个简单的非正则表达式查找和替换不是就足够了吗?这不是你已经在做的吗?我想替换整个,href url到abc.htmlso,你想这样做吗:最终结果:1份调味良好的烤箱烤猪肉嫩嫩肉(配方)
1份欧芹大蒜土豆abc.html(食谱)
请发布您想要的结果好吗?如果您不使用正则表达式,则应该是,而不是。我想替换整型,href url到abc。html如果您不使用正则表达式,则应该是,而不是。我想替换整型,href url到abc。html@khelwood上面的replaceAll更改了index.php的所有内容。@Vijay不,这是wh在
replace
中,可以使用replaceAll
来更改正则表达式“index.php”
,这不是您想要做的。@khelwood尝试了一下,发现这里的replace就足够了。谢谢。别忘了转义“index.php”中的点。
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");
}