目录
一、正则表达式
Regular Expression
正则表达式,简称RegExp
,常规通用的表达式,在多个开发语言中都有它的实现,可以通过正则表达式来快速的检索、匹配、查找、替换字符串中的文本。
边界匹配器
构造 | 匹配 |
---|---|
^ | 行的开头 |
$ | 行的结尾 |
\b | 单词开头或结束 |
\B | 非单词开头或结束 |
字符
构造 | 匹配 |
---|---|
x | 字符 x |
\ | 反义字符 |
字符类
构造 | 匹配 |
---|---|
[abc] | a、b 或 c(简单类) |
[^abc] | 任何字符,除了 a、b 或 c(否定) |
[a-zA-Z] | a 到 z 或 A 到 Z,两头的字母包括在内(范围) |
预定义字符类
构造 | 匹配 |
---|---|
. | 任何字符(与行结束符可能匹配也可能不匹配) |
\d | 数字:[0-9] |
\D | 非数字: [^0-9] |
\s | 空白字符:[ \t\n\x0B\f\r] |
\S | 非空白字符:[^\s] |
\w | 单词字符:[a-zA-Z_0-9] |
\W | 非单词字符:[^\w] |
Greedy 数量词
构造 | 匹配 |
---|---|
? | 一次或一次也没有 |
* | 零次或多次 |
+ | 一次或多次 |
{n} | 恰好 n 次 |
{n,} | 至少 n 次 |
{n,m} | 至少 n 次,但是不超过 m 次 |
Logical 运算符
构造 | 匹配 |
---|---|
XY | X 后跟 Y |
(X) | X,作为捕获组 |
Matches 方法
Matcher.matches
方法,为整块全匹配,字符串完全匹配返回true
。
@Test
public void test03(){
String input = "https://ichochy.com";
//正则表达式,(.+)代表一个或多个字符
String regex = "https://.+.com";
Pattern pattern = Pattern.compile(regex);//编译表达式
Matcher matcher = pattern.matcher(input);//匹配表达式
System.out.println(matcher.matches());//全文匹配返回:true
}
Find 方法
Matcher.find
方法,为查找模式匹配,匹配到就返回true
。
@Test
public void test02(){
String input = "我的网站是:https://ichochy.com,你知道吗?";
//正则表达式,(.+)代表一个或多个字符
String regex = "https://.+.com";
Pattern pattern = Pattern.compile(regex);//编译表达式
Matcher matcher = pattern.matcher(input);//匹配表达式
System.out.println(matcher.find());//查找匹配返回:true
System.out.println(matcher.matches());//全文匹配返回:false
System.out.println(matcher.find());//再次查找匹配返回:false
matcher.reset();//重置匹配器
System.out.println(matcher.find());//重置查找返回:true
}
find
方法多次调用,出现结果不相同的问题:This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.
Matcher.find
方法第一次查找匹配成功后,如果Matcher
没有重置(Matcher.reset()
),则从上一次匹配成功位置的后面开始查找,所以会出现,再次匹配不成功,返回false
。
Group 分组
正则表达式通过括号分组进行匹配,matcher.group(int group)
:通过组序号获取匹配信息
注意:使用Matcher时,必须首先调用matches()判断是否匹配成功,匹配成功后,才能调用group()提取子串。
@Test
public void test01(){
Pattern pattern = Pattern.compile("(\\d{3,4})-([123456789]\\d{6,7})");
System.out.println(pattern.matcher("010-12345678").matches());
System.out.println(pattern.matcher("021-123456").matches());
System.out.println(pattern.matcher("022#1234567").matches());
// 获得Matcher对象:
Matcher matcher = pattern.matcher("010-12345678");
if (matcher.matches()) {
String whole = matcher.group(0); // "010-12345678", 0表示匹配的整个字符串
String area = matcher.group(1); // "010", 1表示匹配的第1个子串
String tel = matcher.group(2); // "12345678", 2表示匹配的第2个子串
System.out.println(whole);
System.out.println(area);
System.out.println(tel);
}
}
小练习:
public class Ch02 {
@Test
public void test06() {
String regex = "[-_]";
String str = "123-4756_qweqwe-7987_465";
String[] split = str.split(regex);
System.out.println(Arrays.toString(split));
}
@Test
public void test05(){
String regex = "\\d";
String str = "1111c2222d456456456f465gh987897";
String s = str.replaceAll(regex, "@");
System.out.println(s);
}
@Test
public void test04() {
String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
String email = "175367745@qq.com";
System.out.println(email.matches(regex));
}
@Test
public void test03() {
String regex = "a";
String str = "cat cat dog dog cat";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
// 统计cat在字符串中出现的次数
int count = 0;
System.out.println(matcher.find(str.length() - 1));
while(matcher.find()){
count++;
}
System.out.println("出现了" + count + "次");
}
@Test
public void test02() {
String regex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
String email = "175367745@qq.";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(email);
System.out.println(matcher.matches());
}
@Test
public void test01() {
String str = "hello,i am from jilin changchun!";
// 必须包含jilin
String pattern = ".*jilina.*";
boolean b = Pattern.matches(pattern,str);
System.out.println("字符串中是否包含了jilin:" + b);
}
}
常用的正则表达式
1、手机号码的校验: ^[1][3,4,5,6,7,8,9][0-9]{9}$
2、身份证的校验:
^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$
3、邮箱的校验:^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$
4、日期 YYYY-MM-DD:^\d{4}(\-)\d{1,2}\1\d{1,2}$
5、日期 YYYY-MM-DD hh:mm:ss :
^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$
6、QQ号的校验:说明:5-11位数字
^[1-9][0-9]{4,10}$