public static void main(String[] args)
{
String s="sadfsdf<B>11</B>sdf1sdf" ;
String a = s.replaceAll("1", "2");
System.out.println();
}我需要把 1替换成2 但是我又不需要替换<B>11</B>这里面1我知道split成数组可以 但是我想用正则表达式搞 谁会?
{
String s="sadfsdf<B>11</B>sdf1sdf" ;
String a = s.replaceAll("1", "2");
System.out.println();
}我需要把 1替换成2 但是我又不需要替换<B>11</B>这里面1我知道split成数组可以 但是我想用正则表达式搞 谁会?
试一下
Pattern p = Pattern.compile("(.*)<B>.*</B>(.*)");
Matcher m = p.matcher(s);
String result = null;
if(m.find()) {
String s1 = m.group(1);
String s2 = m.group(2);
result = s.replace(s1,s1.replaceAll("1","2"));
result = s.replace(s2,s2.replaceAll("1","2"));
}
System.out.println(result);
这样不就行了?
str = str.replaceAll("[^0-9][0-9][^0-9]", "2");
System.out.println(str);
String str = "sadfsdf <B>11 </B>sdf1sdf";
str = str.replaceAll("[^0-9]1[^0-9]", "2");
System.out.println(str);
public static void main(String[] args)
{
String s="s1adfsdf <B>11</B>sdf1sdf" ;
String a = s.replaceAll("1[^(?:<B>\\d*</B>)]", "2");
System.out.println(a);
}
{
String s="s1adfsdf <B>gg11</B>sdf1sdf" ;
String a = s.replaceAll("1(?=[^(?:<B>\\d</B>)])", "2");
System.out.println(a);
}
但是 如果我想要 public static void main(String[] args)
{
String s="sadfsdf <B>11 </B>sdf1s <B>11 </B>df" ;
String a = s.replaceAll("1", "2");
System.out.println();
} 这么替换 好像你们都不行了噢 我的 字符里面可能有很多 <B>11 </B>
String str = "sadfsdf <B>11 </B>sdf1s <B>11 </B>df";
str = str.replaceAll("[^0-9]1[^0-9]", "2");
System.out.println(str);
import java.util.regex.Pattern;public class Test {
public static void main(String[] args) {
String str ="sad1fs1df<B>11</B>sd1f1s<B>11</B>df";
String s = replace(str, "<b>.*?</b>", "1", "2");
System.out.println(s);
}
public static String replace(String str, String exclusionRegex, String orignalRegex, String replacement) {
Pattern pattern = Pattern.compile("((?is:" + exclusionRegex + "))|" + orignalRegex);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
if(matcher.start(1) >= 0) {
matcher.appendReplacement(sb, "$1");
} else {
matcher.appendReplacement(sb, replacement);
}
}
matcher.appendTail(sb);
return sb.toString();
}
}
String s = "sadfsdf <B>11</B>sdf1sdf";
String a = s.replaceAll("1", "2");
String b = a.replaceAll("<B>22</B>", "<B>11</B>");
System.out.println(b);
}