基础
是否符合规则
// 电话号码匹配
// 手机号段只有 13xxx 15xxx 18xxxx 17xxx
System.out.println("18304072984".matches("1[3578]\\d{9}")); // true
正则替换、过滤
/**
* 保留中文、英文、下划线
* [^\u4e00-\u9fa5^a-z^A-Z^0-9^_]
* @param name 姓名
* @return 过滤后的字符串
*/
public static String filterName(String name) {
return name.replaceAll("[^\u4e00-\u9fa5a-zA-Z0-9_]", "_");
}
你好12——_hhh+你
你好12__hhh你
分组匹配
普通分组
// 这个正则表达式有两个组,
// group(0) 是 \\$\\{([^{}]+?)\\}
// group(1) 是 ([^{}]+?)
String regex = "\\$\\{([^{}]+?)\\}";
Pattern pattern = Pattern.compile(regex);
String input = "${name}-babalala-${age}-${address}";
Matcher matcher = pattern.matcher(input);
System.out.println(matcher.groupCount());
// find() 像迭代器那样向前遍历输入字符串
while (matcher.find()) {
System.out.println(matcher.group(0) + ", pos: "
+ matcher.start() + "-" + (matcher.end() - 1));
System.out.println(matcher.group(1) + ", pos: " +
matcher.start(1) + "-" + (matcher.end(1) - 1));
}
1 ${name}, pos: 0-6 name, pos: 2-5 ${age}, pos: 17-22
age, pos: 19-21
${address}, pos: 24-33
address, pos: 26-32
正则分组命名
@Test
public void testLog() {
String text = "第一百四十四条的规定,判决如下: 一、被告黎飞强应于本案判……上诉于广西壮族自治区梧州市中级人民法院。 审 判 长 黄炳友 审 判 员 王 强 人民陪审员 ";
List<String> pre = Arrays.asList("作如下判决:", "判决如下");
// \p{Zs} 全角空格
List<String> suffix = Arrays.asList("审\\s?\\p{Zs}?判\\s?\\p{Zs}?长");
System.out.println(getPatternList(text, pre, suffix));
}
private List<String> getPatternList(String text, List<String> preList, List<String> sufList) {
ArrayList<String> arrayList = new ArrayList<>();
// 临时组名
String groupName = "groupName";
String prePatternStr = preList.stream()
.map(x -> "(" + x + ")")
.collect(Collectors.joining("|", "(", ")"));
String sufPatternStr = sufList.stream().
map(x -> "(" + x + ")")
.collect(Collectors.joining("|", "(", ")"));
String pattern = prePatternStr +
"(?<" + groupName + ">.*?)" +
sufPatternStr;
System.out.println(pattern);
Matcher matcher = Pattern.compile(pattern).matcher(text);
while (matcher.find()) {
arrayList.add(matcher.group(groupName));
}
return arrayList;
}