62,614
社区成员
发帖
与我相关
我的任务
分享
String s="<td href=\"http://www.csdn.net\">csdn is a good csdn!</td><div href=\"http://www.csdn.net\">csdn is a good csdn!</div><a href=\"http://www.csdn.net\">csdn is a good csdn!</a>";
Pattern p=Pattern.compile("(\\<([a-zA-Z]+)\\s+?.*?\\>)(.*?)(?=\\<\\/\\2\\>)");
Matcher m=p.matcher(s);
StringBuffer sb=new StringBuffer();
while(m.find()){
String str=m.group(3);
m.appendReplacement(sb, m.group(1)+str.replaceAll("csdn","参赛队");
}
m.appendTail(sb);
System.out.println(sb);
String s="<td href=\"http://www.csdn.net\">csdn is a good csdn!</td><div href=\"http://www.csdn.net\">csdn is a good csdn!</div><a href=\"http://www.csdn.net\">csdn is a good csdn!</a>";
Pattern p=Pattern.compile("(\\<([a-zA-Z]+)\\s+?.*?\\>)(.*?)(?=\\<\\/\\2\\>)");
Matcher m=p.matcher(s);
StringBuffer sb=new StringBuffer();
while(m.find()){
String str=m.group(3);
Pattern p1=Pattern.compile("csdn");
Matcher m1=p1.matcher(str);
StringBuffer sb1=new StringBuffer();
while(m1.find()){
m1.appendReplacement(sb1, "参赛队");
}
m.appendReplacement(sb, m.group(1)+sb1.toString());
}
System.out.println(sb);
String s = "<a href=\"http://www.csdn.net\">csdn is a good csdn!</a>";
String regex = "(<(\\S+).*?>)(.*?)(</\\2>)";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1)
+ m.group(3).replaceAll("csdn", "参赛队") + m.group(4));
}
String s="<a href=\"http://www.csdn.net\">csdn is a good csdn!</a><a href=\"http://www.csdn.net\">csdn is a good csdn!</a><a href=\"http://www.csdn.net\">csdn is a good csdn!</a>";
Pattern p=Pattern.compile("(\\<a.*?\\>)(.*?)(?=\\<\\/a\\>)");
Matcher m=p.matcher(s);
StringBuffer sb=new StringBuffer();
while(m.find()){
String str=m.group(2);
m.appendReplacement(sb, str.replaceAll("csdn", m.group(1)+"参赛队"));
}
System.out.println(sb);
replaceAll("(\\<a.*?\\>)(csdn)(?=\\<\\/a\\>)", "$1参赛队");
s.replaceAll("(<(\\S+).*?>).*?(</\\2>)", "$1替换$3")