String str="我JSp查看tomcat内存使用情况";
str=str.replaceFirst("JS", "");
System.out.println(str);我希望得到的效果是这样的,用js不能匹配JSp,同样Sp也不能够匹配JSp,
只能通过jsp,或者Jsp,或者JSp才可以匹配到
我用了 正则表达式中的/b好像不行,主要英文单词和中文中间是没有空格这类的分隔符的

解决方案 »

  1.   

    str=str.replaceAll("(?<![a-zA-Z])JS(?![a-zA-Z])", "");
      

  2.   


    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class Zhengze { public void test(){

    String regEx ="[Jj][Ss][Pp]";

    String Str="我JSp查看tomcat内存使用情况";

    System.out.println(IsMatching(Str, regEx));

    String str=Str.replaceFirst(regEx, "");

            System.out.println(str); }


    /**
     * 判断Str是否匹配regEx表达式
     * @param Str
     * @param regEx
     * @return boolean true:匹配成功
     */
    private boolean IsMatching(String Str, String regEx) {
    boolean result =false;
    try{
    Pattern p = Pattern.compile(regEx);
    Matcher m = p.matcher(Str);
    result = m.find();
    }catch(Exception e){

    }
    return result;
    }

    public static void main(String args[]){

    new Zhengze().test();

    }
    }
      

  3.   

    哦,二楼的朋友你写得太死了,这个肯定要通用啊,今天这里的列子是jsp,我也需要用到其他的英文单词
    PS:一楼的还不错,期待更优的解决方法
      

  4.   


       String str="我JSp查看tomcat内存使用情况";
            str=str.replaceFirst("(?<![a-zA-Z])(?i:jsp)(?![a-zA-Z])", "");
            System.out.println(str);
      

  5.   

    String str="我JSp查看tomcat内存使用情况";
    System.out.println(str.replaceAll("(?i)^.*(jsp).*$", "$1"));
    System.out.println(str.replaceAll("(?i)^.*(Jsp).*$", "$1"));
    System.out.println(str.replaceAll("(?i)^.*(jSP).*$", "$1"));--------
    JSp
    JSp
    JSp
      

  6.   

    package com.spring.mvc;import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class csdn3 {
    public static void main(String args[]){
    String str = "我JSp查看tomcat内存使用情况";
    String input = "jsp";
    String regex = "jsp";
    if(Pattern.matches(regex,input.toLowerCase())){
    System.out.println("....");
    Matcher m = Pattern.compile(".*(jsp).*").matcher(str.toLowerCase());
    while(m.find()){
    str = str.toLowerCase().replaceAll("jsp", input);
    }
    }
    System.out.println(str);
    }
    }
      

  7.   

    如果只要求无论大小写都匹配,然后进行替换,那就别用正则表达式,绝对写不出通用表达式来。用正则操作类就成了:
    /**
     * 替换Str中匹配regEx表达式的内容 
     * @param Str
     * @param regEx
     * @return String 替换结果 
     */
    private String ReplaceRegEx(String Str, String regEx) {
    StringBuffer buf = new StringBuffer(); 
    try{
    Pattern p = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);//忽略大小写
    Matcher m = p.matcher(Str);     
    while(m.find()){
    m.appendReplacement(buf, "");//替换成""
    }
    m.appendTail(buf);  //加上尾巴  
    }catch(Exception e){

    }
    return buf.toString();
    }
      

  8.   

    String str="我JSp查看tomcat内存使用情况";
    str=str.replaceFirst("\\b(?i:JS)\\b", "");
    System.out.println(str);
      

  9.   

    上面那个有问题,更正:str=str.replaceAll("(?<=\\W)(?i:JS)(?=\\w)", "");
      

  10.   

    火龙果的JS已经接近我的答案,最后我的修改版是String str="我JSp查看tomcat内存使用情况";
    str=str.replaceFirst("(?<=\\W)(?i:to)(?=\\W)", "");
    System.out.println(str);