测试举例
ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
网小小 大不了 一二山
1234567890
*_%^.
fkdsl232 kdl 89kf dk2 dk
3.14
3-14
1.匹配任意字符(.)
g:全文匹配,没g的话只匹配第一次出现的那个
2.匹配字母和数字
3.匹配数字
4.匹配空白字符
5.字符集合
6.重复一次或多次(+)
7.匹配0个或多个(*)
8.匹配0个或1个(?)
9.分组
@Test
public void test1(){
Pattern compile = Pattern.compile("([a-z]+)([0-9]+)");
Matcher matcher = compile.matcher("aksh22**fdsk21576*23*kl");
while (matcher.find()){
System.out.println("matcher = " + matcher.group());
System.out.println("matcher1 = " + matcher.group(1));
System.out.println("matcher2 = " + matcher.group(2));
}
System.out.println(matcher.groupCount());
}
^(([a-zA-Z]{2}([\u4E00-\u9FA5]{1}|[\d]{2}|))|[\u4E00-\u9FA5]{1}[a-zA-Z])([\d\u4E00-\u9FA5]{1}[\d]{3}[\da-zA-Z]|[\da-zA-Z]{7})$
捕获分组,会把各个分组给缓冲起来,非捕获是不会的