1:正则表达式:就是符合一定规则的字符串
1.常见规则
A:字符
x 字符 x。举例:'a'表示字符a
\\ 反斜线字符。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。.字符本身,用 \.表示
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
2.常见功能:
A:判断功能
String类的public boolean matches(String regex)
B:分割功能
String类的public String[] split(String regex)
C:替换功能
String类的public String replaceAll(String regex,String replacement)
例如把字符串中的所有数字用@代替:
String s = "hello my qq is 545286799";
String regex = "\\d+";
String replaceStr = "@";
String result = s.replaceAll(regex,replaceStr);//result:hello my qq is @ 如果regex = "\\d"; reslut:hello my qq is @@@@@@@@@
D:获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");Matcher m = p.matcher("aaaaab
find():查找存不存在(一定要先find()后才能group())
group():获取刚才查找过的数据
例如把字符串s中有五个字母组成的单词找出来,
String s = "chuang qian ming yue guang,yi shi di shang shuang,ju tou wang ming yue, di tou si gu xiang";
String regex = "\\b\\w{5}\\b";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}