一、特殊字符转义
点的转义:. ==> \\u002E
美元符号的转义:$ ==> \\u0024乘方符号的转义:^ ==> \\u005E
左大括号的转义:{ ==> \\u007B
左方括号的转义:[ ==> \\u005B
左圆括号的转义:( ==> \\u0028
竖线的转义:| ==> \\u007C
右圆括号的转义:) ==> \\u0029
星号的转义:* ==> \\u002A
加号的转义:+ ==> \\u002B
问号的转义:? ==> \\u003F
反斜杠的转义:\ ==> \\u005C
二、字符
中文:\u4e00-\u9fa5
空格:\\s
点(.)://.(或\\.)
三、实例
3.1 匹配xml标签元素
String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<RESPONSE><MSGBODY><SESSIONID>09ccacab7be538856cc925306e6f912e</SESSIONID><USERNAME>svili</USERNAME>" + "<TOKEN>123456</TOKEN><STATUS>0000</STATUS></MSGBODY></RESPONSE>";
String regex = "<(/*[A-Za-z]+)>"; Pattern p = Pattern.compile(regex); Matcher matcher = p.matcher(xmlStr); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, "<" + matcher.group(1).toUpperCase() + ">"); } matcher.appendTail(sb);3.2 匹配数据集合
//对字段名做一些处理:${B1073034_201602}${B1073014_201602} String fields_regex = "\\u0024\\u007B([A-Z_a-z0-9]+)}"; Matcher fields_matcher = Pattern.compile(fields_regex).matcher(fields); StringBuffer fieldsBuffer = new StringBuffer(); while (fields_matcher.find()) { fields_matcher.appendReplacement(fieldsBuffer, "," + fields_matcher.group(1)); } //fields_matcher.appendTail(fieldsBuffer); //对字段值做一些处理:#{字段值:汉字,空,时间,字符,数字_-},#{} String values_regex = "#\\u007B([\u4e00-\u9fa5A-Z_a-z0-9//.\\s:-]*)}"; Matcher values_matcher = Pattern.compile(values_regex).matcher(values); StringBuffer valuesBuffer = new StringBuffer(); while (values_matcher.find()) { values_matcher.appendReplacement(valuesBuffer, values_matcher.group(1)); } //values_matcher.appendTail(valuesBuffer);