常用正则标记
1.+ 代表出现一次或多次
2.* 代表出现0次或多次
3.?代表出现0次或1次
4. . 通配符
5. \ 转义字符
6. 匹配除换行符外的任意字符
4.[] 匹配中括号中的任意一个字符,常与1、2、3连用
5.[^] 匹配除了中括号中的字符的任意字符
6.A-Z 代表着从A到Z的所有元素
7.\s 空白/换行/制表符 ; \S 非 空白/换行/制表符
8.\w 匹配a-zA-Z_
9.\t 制表符
10.\d 数字 ;\D 非数字
11.()与[] 标记一个子表达式的开始和结束位置
12.$ 匹配输入字符串的结尾位置
13.a{n} 匹配n个a
14.a{n,} 匹配n+个a
15.a{n,m} 匹配[n,m]个a
Java String支持
public String[] split(String regex, int limit)
public String[] split(String regex)
public boolean matches(String regex)
public String replaceAll(String regex, String replacement)
public String replaceFirst(String regex, String replacement)
以上函数源码是调用Pattern类中的函数
java util regex包
Pattern类
Pattern.compile(regex) //编译正则表达式
public static boolean matches(String regex, CharSequence input)
public String[] split(CharSequence input, int limit)
public String[] split(CharSequence input)
public Matcher matcher(CharSequence input)
Matcher类
通过pattern,matcher(str)获取
public boolean matches()
public String replaceAll(String replacement)
public String replaceFirst(String replacement)
public String group(int group) //分组group()或group(0)匹配整个字符串
//group(n)为匹配成功后,匹配串第n个括号中的内容
//其实就是将Matcher类转化为String类
样例: