试试Pattern类的compile方法compile public static Pattern compile(String regex, int flags)Compiles the given regular expression into a pattern with the given flags. Parameters: regex - The expression to be compiled flags - Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, and CANON_EQ Throws: IllegalArgumentException - If bit values other than those corresponding to the defined match flags are set in flags PatternSyntaxException - If the expression's syntax is invalid
public static Pattern compile(String regex,
int flags)Compiles the given regular expression into a pattern with the given flags.
Parameters:
regex - The expression to be compiled
flags - Match flags, a bit mask that may include CASE_INSENSITIVE, MULTILINE, DOTALL, UNICODE_CASE, and CANON_EQ
Throws:
IllegalArgumentException - If bit values other than those corresponding to the defined match flags are set in flags
PatternSyntaxException - If the expression's syntax is invalid
String search = "j2ee";
String repl = "<font color='red'>" + search + "</font>";Pattern p = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);String str = m.replaceAll(repl);
str = str.replaceAll("(J|j)2(E|e)(E|e)",repl);
String repl = "<font color='red'>" + search + "</font>";因为你使用了这两个式子,这就决定了,你替换后,所有的j2ee都变成小写的了,
这和一楼说的有什么区别。
我刚才GOOGLE一下,java查出来有Java,也有java。
我要的就是这个效果,多谢各位帮忙。
String search="j2ee";Pattern p = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);StringBuffer sb = new StringBuffer();
while (m.find())
{
m.appendReplacement(sb, "<font color=\"red\">"+str.substring(m.start(), m.end())+"</font>");
}m.appendTail(sb);
System.out.println(sb.toString());