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成数组可以   但是我想用正则表达式搞   谁会?

解决方案 »

  1.   

    String a = s.replaceAll("([^>]1)", "2"); 
    试一下
      

  2.   

     String s="sadfsdf <B>11 </B>sdf1sdf" ; 
     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);
      

  3.   

    如果单是这个问题,String a = s.replaceAll("sdf1", "sdf2");
    这样不就行了?
      

  4.   

    String str = "sadfsdf <B>11 </B>sdf1sdf";
    str = str.replaceAll("[^0-9][0-9][^0-9]", "2");
    System.out.println(str);
      

  5.   

    写错了,这个才对
    String str = "sadfsdf <B>11 </B>sdf1sdf";
    str = str.replaceAll("[^0-9]1[^0-9]", "2");
    System.out.println(str);
      

  6.   


    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);
    }
      

  7.   

    更正: public static void main(String[] args)
    {
      String s="s1adfsdf <B>gg11</B>sdf1sdf" ;
      String a = s.replaceAll("1(?=[^(?:<B>\\d</B>)])", "2"); 
              System.out.println(a);
    }
      

  8.   

    各位的好像都行 
    但是 如果我想要  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>
      

  9.   

    谁说我的不行?
         String str = "sadfsdf <B>11 </B>sdf1s  <B>11 </B>df"; 
         str = str.replaceAll("[^0-9]1[^0-9]", "2"); 
         System.out.println(str); 
      

  10.   

    import java.util.regex.Matcher;
    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();
        }
    }
      

  11.   

     一个个都是笨蛋啊。 还学java。哎....
      

  12.   

    public static void main(String[] args) {
    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);
    }