java正则表达式,将字符串中\后的第一个字母变成大写

时间:2023-01-07 09:08:07

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);