java正則表達式,將字符串中\后的第一個字母變成大寫
例子是比較簡單,注意的是java中的“\\”意義是:我要插入一個正則表達式的反斜線,所以其后面的字符有特殊有意義。所以普通反斜線應該是"\\\\"
String in = "\\a\\bnf\\fv"; System.out.println("in is= " + in); StringBuffer sb = new StringBuffer(); Pattern p = Pattern.compile("\\\\[a-z|A-Z]"); Matcher m = p.matcher(in); while (m.find()) { // Find each match in turn; String can't do this. //String name = m.group(1); // Access a submatch group; String can't do this. m.appendReplacement(sb, "\\" + m.group().toUpperCase()); System.out.println("m.group() is= " + m.group()); } m.appendTail(sb); System.out.println("sb is= " + sb);