参考:
普通匹配
目的 | 表达式 | 例子 |
---|---|---|
匹配单个字符 | . | a.o匹配abo,aco,ado…,不能匹配abbo |
匹配前面任意个表达式(尽可能多) | * | a*o匹配o,ao,aao,aaao |
匹配前面至少1个表达式(尽可能多) | + | a+o匹配ao, aao,aaao,不能匹配o |
仅在字符串出现在字符串或一行的开头匹配 | ^ | ^car匹配carmove,不匹配movecar |
仅在字符串出现在文件的结尾时匹配 | $ | car$仅在car出现在文件末尾时匹配 |
匹配任何文字字符 | \w | a\wd匹配add和a1d,但不匹配a d |
匹配空白字符 | \s | a\sd匹配a d |
匹配十进制字符 | \d | \d匹配"wd03"中的"0"和"3" |
匹配集合中的任何字符 | [abc] | a[bcd]匹配ab,ac,ad |
匹配范围中的任何字符 | [a-z] | a[n-t]匹配ao,ap,aq |
捕获并为括号内的字符隐式编号 | () | ([a-z])x\1匹配abxab,baxba但不匹配abba,因为\1指的是第一个括号中的内容 |
指定前一个字符出现次数 | {x} | x(ab){2}x匹配"xababx" |
匹配前面任意个表达式(尽可能少) | *? | \w*?d匹配"faded"中的"ed"和“fad”,但不会匹配“faded” |
匹配前面至少一个表达式(尽可能少) | +? | e\w+?匹配“faded”中"ed" |
?xxx
(?=pattern) | 正向肯定预查 | exp1(?=exp2) | 前瞻:查找exp2前面的exp1 |
(?!pattern) | 正向否定预查 | exp1(?!exp2) | 负前瞻:查找后面不是exp2的exp1 |
(?<=pattern) | 反向肯定预查 | (?<=exp2)exp1 | 后顾:查找exp2后面的exp1 |
(?<!pattern) | 反向否定预查 | (?<!exp2)exp1 | 负后顾:查找前面不是exp2的exp1 |
(?:pattern) | 匹配patter | industr(?:y|ies) | 相当于 industry|industries |
懒惰匹配
a.*?b
匹配最短的,以a开始,以b结束的字符串。如果把它应用于aabab的话,它会匹配aab(第一到第三个字符)和ab(第四到第五个字符)
正则表达式的应用
- 编译正则表达式的字符串值构造对应的模式Pattern对象
- 创建匹配给定输入与此模式的匹配器Matcher
- 通过匹配器对象执行操作,匹配器对象的方法很丰富,互相组合使用更加强大(JDK在线API)
1、去掉字符串中的空格和换行符
public static String getNonBlankStr(String str) {
if(str!=null && !"".equals(str)) {
Pattern pattern = Pattern.compile("\\s*|\t|\r|\n"); //去掉空格符合换行符
Matcher matcher = pattern.matcher(str);
String result = matcher.replaceAll("");
return result;
}else {
return str;
}
}
2、去掉指定特殊字符
public static String StringFilter(String str) throws PatternSyntaxException {
// String regEx = "[^a-zA-Z0-9]"; // 只允许字母和数字
// 清除掉所有特殊字符(除了~之外)
String regEx="[`!@#$%^&*()+=|{}':;',//[//].<>/?!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(str);
return matcher.replaceAll("").trim();
}
3、Pattern.matches()检查字符串中是否存在指定字符
String text = "there are many hotels " +"by amap.the adrr: http://xxxx pattern.";
String pattern = ".*http://.*";
boolean matches = Pattern.matches(pattern, text);//true则存在
String text = "sadfsaaasdfjdsabbbasdfsaccc";
String pattern = ".?(aaa|bbb|ccc).*";
boolean matches = Pattern.matches(pattern, text);//true则存在
4、Pattern.split()用正则表达式作为分隔符,把文本分割为String类型的数组
/**
* 结果:element = grjk Text
* element = wwwdsf
* element = Many
* element = egsdg r geg
*/
String text = "A reg grjk Text reg wwwdsf reg Many reg egsdg r geg";
String patternString = "reg";
Pattern pattern = Pattern.compile(patternString);
String[] split = pattern.split(text);
for(String element : split){
System.out.println("element = " + element);
}
5、Matcher 实例的find() + start() + end()寻找字符串中指定字符串出现的次数和起始和结束的索引位置
/**
* 结果:found: 1 : 2 - 4
* found: 2 : 5 - 7
* found: 3 : 23 - 25
* found: 4 : 70 - 72
*/
String text ="This is the text which is to be searched " +"for occurrences of the word 'is'.";
String patternString = "is";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);
int count = 0;
while(matcher.find()) {
count++;
System.out.println("found: " + count + " : " + matcher.start() + " - " + matcher.end());
}
6、Matcher 匹配指定格式的特殊字符串
Pattern pattern = Pattern.compile("[0-9]*");//判断是否都是数字
Matcher isNum = pattern.matcher("1123是数字");
if(isNum.matches()) {
System.out.println("全部是数字");
} else {
System.out.println("有汉字");
}
//匹配任意字符串中包含形如“@数字&”格式的子串
String st="看,墙面上的雕塑是什么?@322&大家一定猜到了,这是一个神经元。汇区的研院校及企业资源,促进产学研医多个环节上的相互合作与交融";
Pattern pattern = Pattern.compile("^(.*)@-?[1-9]\\d*&(.*)$"); //去掉空格符合换行符
Matcher matcher = pattern.matcher(st);
boolean result = matcher.find();