正则实现:字符串:中华人民,大华人们,我们华人一起,华人替换关键字“华人” 条件:除去'中华人民','大华人们'等词组外,所有'华人'的词组
均替换成AA例如上面字符串 替换后是:
中华人民,大华人们,我们AA一起,AA

解决方案 »

  1.   


    String str = "中华人民,大华人们,我们华人一起,华人";

    Matcher matcher = Pattern.compile("华人").matcher(str);
    StringBuffer sbr = new StringBuffer();
    while (matcher.find()) {
    int i = matcher.start();
    if(i+3 < str.length()-1 && (str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
    continue;
        matcher.appendReplacement(sbr, "Java");
    }
    matcher.appendTail(sbr);
    System.out.println(sbr.toString());
      

  2.   

    不用这样 子  其实 你的需求很好实现 的  
    String  str = "中华人民,大华人们,我们华人一起,华人"
    str  = str .sunbstring(9,str.length()).replaceAll("华人","AA");
      

  3.   

    貌似还忘了一个判断 重新发下if判断
    if(i+3 < str.length()-1 && i-1 > 0 &&(str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
      

  4.   


    public static void main(String[] args) {
    String s = "中华人民,大华人们,我们华人一起,华人";
    String r = "(,?)(.*?华人.*?)(,?)";
    Pattern p = Pattern.compile(r);
    Matcher m = p.matcher(s);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
    String news = m.group(1) + "AA" + m.group(3);
    m.appendReplacement(sb, news);
    }
    m.appendTail(sb);
    System.out.println(sb);
    }
      

  5.   


    String str ="中华人民,大华人们,我们华人一起,华人";
    String regex1= "((?<!\u4e2d)\u534e\u4eba(?!\u6c11)|(?<!\u4e2d)\u534e\u4eba(?=\u6c11)|(?<=\u4e2d)\u534e\u4eba(?!\u6c11))";
    String regex2= "((?<!\u5927)\u534e\u4eba(?!\u4eec)|(?<!\u5927)\u534e\u4eba(?=\u4eec)|(?<=\u5927)\u534e\u4eba(?!\u4eec))";
    Matcher m1 = Pattern.compile(regex1).matcher(str);
    Matcher m2 = Pattern.compile(regex2).matcher(str);
    while(m2.find()&&m1.find()){
    int i1 = m1.start();
    int i2 = m2.start();
    if(i1==i2){
    str = str.substring(0, i1)+"AA"+str.substring(m1.end(), str.length());
    }
    System.out.println(str);
    }
      

  6.   


    弱弱的问一下 ,这里的 u6c11 u4eba 都是啥
      

  7.   

    u6c11 u4eba 是 汉字的unicode编码
      

  8.   

    我上面的发现不对
    改成这样String str ="中华人民,大华人们,我们华人一起,华人";
    String regex1= "((?<!\u4e2d)\u534e\u4eba(?!\u6c11)|(?<!\u4e2d)\u534e\u4eba(?=\u6c11)|(?<=\u4e2d)\u534e\u4eba(?!\u6c11))";
    String regex2= "((?<!\u5927)\u534e\u4eba(?!\u4eec)|(?<!\u5927)\u534e\u4eba(?=\u4eec)|(?<=\u5927)\u534e\u4eba(?!\u4eec))";
    Matcher m1 = Pattern.compile(regex1).matcher(str);
    Matcher m2 = Pattern.compile(regex2).matcher(str);
    while(m2.find()){
    int i2 = m2.start();
    while(m1.find()){
    int i1 = m1.start();
    if(i1==i2){
    str = str.substring(0, i1)+"AA"+str.substring(m1.end(), str.length());
    }
    }
    m1.reset();
    System.out.println(str);
    }
      

  9.   

    有个想法,
    先找到所有“华人”字段的index
    然后找所有中华人民,大华人们的index
    然后中华人民和大华人们的index+1
    然后和华人的index匹配,
    之后根据华人中所有没有匹配到的index,把对应的“华人”删除
      

  10.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class TextRegexZhongHua {
         
    public static void main(String args[]) {
    String str="水木年华人都喜欢,中华人民,大华人们,我们华人一起,华人";
    Matcher m=Pattern.compile("(?<![\u4e2d\u5927])(?<!(\u6c34\u6728\u5e74))(\u534e\u4eba)(?![\u6c11\u4eec])").matcher(str);
    //找到前面没有水木年、中、大并且后面没有民、们的华人两字
    while(m.find()) {
    str=str.substring(0,m.start())+"AA"+str.substring(m.end());
    }
    System.out.println(str);
    }
    }测试结果:水木年华人都喜欢,中华人民,大华人们,我们AA一起,AA按照上面这个例子,你可以按自己的需求来替换关键字,注意使用JDK自带的native2ascii.exe工具,还有就是不要使用replace(String regex,"AA");这种方法,它和(?<!)这种符号使用时很不理想..
      

  11.   


    String str = "中华人民,大华人们中华人民,我们华人一起,华人";
    Matcher matcher = Pattern.compile("华人").matcher(str);
    StringBuffer sbr = new StringBuffer();
    while (matcher.find()) {
    int i = matcher.start();
    if(i+3 < str.length()-1 && i-1 > -1 &&(str.substring(i-1,i+3).equals("中华人民") || str.substring(i-1,i+3).equals("大华人们")))
    continue;
        matcher.appendReplacement(sbr, "AA");
    }
    matcher.appendTail(sbr);
    System.out.println(sbr.toString());
    把我的重发一遍,如果还有其他的词不需要替换,可以考虑把if判断的或者里面的判断拿出来写成一个方法。
      

  12.   

    恩,如果前面有“中”字的华人,你也要换,那么就像你做那种综合比较了import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class TextRegexZhongHua {
         
        public static void main(String args[]) {
            String str="水木年华人都喜欢,中华人民,大华人们,我们华人一起,华人,中华人们,大华人民";
            Matcher m1=Pattern.compile("(((?<![中])(?<!水木年)华人(?![民]))|((?<=[中])(?<!水木年)华人(?![民]))|((?<![中])(?<!水木年)华人(?=[民])))").matcher(str);
            //找到前面没有水木年、中、大并且后面没有民、们的华人两字
            
            Matcher m2=Pattern.compile("(((?<![大])(?<!水木年)华人(?![们]))|((?<=[大])(?<!水木年)华人(?![们]))|((?<![大])(?<!水木年)华人(?=[们])))").matcher(str);
            while(m1.find()) {
             while(m2.find()) {
             if(m1.start()==m2.start()) {
             str=str.substring(0,m1.start())+"AA"+str.substring(m1.end());
             }
             }
             m2.reset();
            }
            System.out.println(str);
        }
    }
    测试结果:水木年华人都喜欢,中华人民,大华人们,我们AA一起,AA,中AA们,大AA民