String str="我JSp查看tomcat内存使用情况";
str=str.replaceFirst("JS", "");
System.out.println(str);我希望得到的效果是这样的,用js不能匹配JSp,同样Sp也不能够匹配JSp,
只能通过jsp,或者Jsp,或者JSp才可以匹配到
我用了 正则表达式中的/b好像不行,主要英文单词和中文中间是没有空格这类的分隔符的
str=str.replaceFirst("JS", "");
System.out.println(str);我希望得到的效果是这样的,用js不能匹配JSp,同样Sp也不能够匹配JSp,
只能通过jsp,或者Jsp,或者JSp才可以匹配到
我用了 正则表达式中的/b好像不行,主要英文单词和中文中间是没有空格这类的分隔符的
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();
}
}
PS:一楼的还不错,期待更优的解决方法
String str="我JSp查看tomcat内存使用情况";
str=str.replaceFirst("(?<![a-zA-Z])(?i:jsp)(?![a-zA-Z])", "");
System.out.println(str);
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
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);
}
}
/**
* 替换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();
}
str=str.replaceFirst("\\b(?i:JS)\\b", "");
System.out.println(str);
str=str.replaceFirst("(?<=\\W)(?i:to)(?=\\W)", "");
System.out.println(str);