字符类
[abc]:代表a或者b,或者c字符中的一个。
[^abc]:代表除a,b,c以外的任何字符。
[a-z]:代表a-z的所有小写字符中的一个。
[A-Z]:代表A-Z的所有大写字符中的一个。
[0-9]:代表0-9之间的某一个数字字符。
[a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
[a-dm-p]:a 到 d 或 m 到 p之间的任意一个字符。
package com.example.demo1;
public class a {
public static void main(String[] args) {
// 1. [abc]:代表a或者b,或者c字符中的一个。
testRegex("[abc]", "a"); // 匹配
testRegex("[abc]", "d"); // 不匹配
// 2. [^abc]:代表除a,b,c以外的任何字符。
testRegex("[^abc]", "a"); // 不匹配
testRegex("[^abc]", "d"); // 匹配
// 3. [a-z]:代表a-z的所有小写字符中的一个。
testRegex("[a-z]", "a"); // 匹配
testRegex("[a-z]", "A"); // 不匹配
// 4. [A-Z]:代表A-Z的所有大写字符中的一个。
testRegex("[A-Z]", "A"); // 匹配
testRegex("[A-Z]", "a"); // 不匹配
// 5. [0-9]:代表0-9之间的某一个数字字符。
testRegex("[0-9]", "5"); // 匹配
testRegex("[0-9]", "a"); // 不匹配
// 6. [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之间的任意一个字符。
testRegex("[a-zA-Z0-9]", "a"); // 匹配
testRegex("[a-zA-Z0-9]", "A"); // 匹配
testRegex("[a-zA-Z0-9]", "5"); // 匹配
testRegex("[a-zA-Z0-9]", "@"); // 不匹配
// 7. [a-dm-p]:a 到 d 或 m 到 p之间的任意一个字符。
testRegex("[a-dm-p]", "c"); // 匹配
testRegex("[a-dm-p]", "n"); // 匹配
testRegex("[a-dm-p]", "h"); // 不匹配
}
/**
* 测试正则表达式匹配结果
*
* @param regex 正则表达式
* @param input 输入字符串
*/
public static void testRegex(String regex, String input) {
// 使用 String 的 matches 方法进行匹配
if (input.matches(regex)) {
System.out.println("正则表达式 " + regex + " 匹配输入字符串 " + input);
} else {
System.out.println("正则表达式 " + regex + " 不匹配输入字符串 " + input);
}
}
}
逻辑运算符
&&:并且
| :或者
\ :转义字符
package com.example.demo1;
public class a {
public static void main(String[] args) {
// 1. 使用 | 运算符(或者)
testRegex("a|b", "a"); // 匹配
testRegex("a|b", "b"); // 匹配
testRegex("a|b", "c"); // 不匹配
// 2. 使用 && 运算符(并且)
testRegex("[a-z&&[^bc]]", "a"); // 匹配 a-z 中除 b 和 c 以外的字符
testRegex("[a-z&&[^bc]]", "b"); // 不匹配
testRegex("[a-z&&[^bc]]", "c"); // 不匹配
testRegex("[a-z&&[^bc]]", "d"); // 匹配
// 3. 使用转义字符 \
testRegex("\\.", "."); // 匹配点号
testRegex("\\d", "5"); // 匹配任意数字,等价于 [0-9]
testRegex("\\d", "a"); // 不匹配
testRegex("\\w", "a"); // 匹配任意单词字符,等价于 [a-zA-Z_0-9]
testRegex("\\w", "@"); // 不匹配
}
/**
* 测试正则表达式匹配结果
*
* @param regex 正则表达式
* @param input 输入字符串
*/
public static void testRegex(String regex, String input) {
// 使用 String 的 matches 方法进行匹配
if (input.matches(regex)) {
System.out.println("正则表达式 " + regex + " 匹配输入字符串 " + input);
} else {
System.out.println("正则表达式 " + regex + " 不匹配输入字符串 " + input);
}
}
}
预定义字符
"." : 匹配任意单个字符
"\d":任何数字[0-9]的简写
"\D":任何非数字[^0-9]的简写
"\s": 空白字符:[ \t\n\x0B\f\r] 的简写
"\S": 非空白字符:[^\s] 的简写
"\w":单词字符:[a-zA-Z_0-9]的简写
"\W":非单词字符:[^\w]
package com.example.demo1;
public class a {
public static void main(String[] args) {
// 1. "." : 匹配任意单个字符
testRegex(".", "a");
testRegex(".", "1");
testRegex(".", " ");
testRegex(".", "@");
// 2. "\d":任何数字[0-9]的简写
testRegex("\\d", "5");
testRegex("\\d", "a");
// 3. "\D":任何非数字[^0-9]的简写
testRegex("\\D", "a"); // 匹配结果:匹配
testRegex("\\D", "5"); // 匹配结果:不匹配
// 4. "\s": 空白字符:[ \t\n\x0B\f\r] 的简写
testRegex("\\s", " "); // 匹配结果:匹配
testRegex("\\s", "\t"); // 匹配结果:匹配
testRegex("\\s", "a"); // 匹配结果:不匹配
// 5. "\S": 非空白字符:[^\s] 的简写
testRegex("\\S", "a"); // 匹配结果:匹配
testRegex("\\S", " "); // 匹配结果:不匹配
// 6. "\w":单词字符:[a-zA-Z_0-9]的简写
testRegex("\\w", "a"); // 匹配结果:匹配
testRegex("\\w", "5"); // 匹配结果:匹配
testRegex("\\w", "@"); // 匹配结果:不匹配
// 7. "\W":非单词字符:[^\w]
testRegex("\\W", "@"); // 匹配结果:匹配
testRegex("\\W", "a"); // 匹配结果:不匹配
}
/**
* 测试正则表达式匹配结果
*
* @param regex 正则表达式
* @param input 输入字符串
*/
public static void testRegex(String regex, String input) {
// 使用 String 的 matches 方法进行匹配
if (input.matches(regex)) {
System.out.println("正则表达式 " + regex + " 匹配输入字符串 " + input);
} else {
System.out.println("正则表达式 " + regex + " 不匹配输入字符串 " + input);
}
}
}
数量词
X? : 0次或1次
X* : 0次到多次
X+ : 1次或多次
X{n} : 恰好n次
X{n,} : 至少n次
X{n,m}: n到m次(n和m都是包含的)
package com.example.demo1;
public class a {
public static void main(String[] args) {
// 1. X? : 0次或1次
testRegex("a?", ""); // 匹配 0 次 a
testRegex("a?", "a"); // 匹配 1 次 a
testRegex("a?", "aa"); // 不匹配,超过 1 次 a
// 2. X* : 0次到多次
testRegex("a*", ""); // 匹配 0 次 a
testRegex("a*", "a"); // 匹配 1 次 a
testRegex("a*", "aaaa"); // 匹配多次 a
// 3. X+ : 1次或多次
testRegex("a+", ""); // 不匹配,0 次 a
testRegex("a+", "a"); // 匹配 1 次 a
testRegex("a+", "aaaa"); // 匹配多次 a
// 4. X{n} : 恰好n次
testRegex("a{3}", "aaa"); // 匹配恰好 3 次 a
testRegex("a{3}", "aa"); // 不匹配,少于 3 次 a
testRegex("a{3}", "aaaa"); // 不匹配,多于 3 次 a
// 5. X{n,} : 至少n次
testRegex("a{3,}", "aaa"); // 匹配至少 3 次 a
testRegex("a{3,}", "aa"); // 不匹配,少于 3 次 a
testRegex("a{3,}", "aaaa"); // 匹配多于 3 次 a
// 6. X{n,m}: n到m次(n和m都是包含的)
testRegex("a{2,4}", "aa"); // 匹配 2 次 a
testRegex("a{2,4}", "aaa"); // 匹配 3 次 a
testRegex("a{2,4}", "aaaa"); // 匹配 4 次 a
testRegex("a{2,4}", "a"); // 不匹配,少于 2 次 a
testRegex("a{2,4}", "aaaaa"); // 不匹配,多于 4 次 a
}
/**
* 测试正则表达式匹配结果
*
* @param regex 正则表达式
* @param input 输入字符串
*/
public static void testRegex(String regex, String input) {
// 使用 String 的 matches 方法进行匹配
if (input.matches(regex)) {
System.out.println("正则表达式 " + regex + " 匹配输入字符串 " + input);
} else {
System.out.println("正则表达式 " + regex + " 不匹配输入字符串 " + input);
}
}
}
本地数据爬取示例
爬取文本中的邮箱和手机号
package com.example.demo1;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class a {
public static void main(String[] args) {
// 待爬取的长文本(包含多个邮箱和手机号)
String text = "用户信息:张三 电话13812345678,邮箱zhangsan@example.com;" +
"李四 电话15998765432,邮箱lisi.work@foxmail.cn;" +
"王五 电话18655551234,邮箱wangwu-2024@163.com;" +
"赵六 电话17700009999,邮箱zhaoliu@yeah.net;" +
"测试备用号13144445555,测试邮箱test.user@outlook.com.tw";
// 提取手机号:1开头,第二位3-9,后面9位数字(简化版正则)
String phoneRegex = "1[3-9]\\d{9}";
// 提取邮箱:用户名(字母数字下划线.-)@ 域名(字母数字.-). 顶级域名(2-6位字母)
String emailRegex = "[\\w.-]+@[\\w.-]+\\.\\w{2,6}";
// 演示爬取手机号
System.out.println("===== 爬取到的手机号 =====");
extractByRegex(text, phoneRegex);
// 演示爬取邮箱
System.out.println("\n===== 爬取到的邮箱 =====");
extractByRegex(text, emailRegex);
}
/**
* 通用提取方法:根据正则表达式从文本中提取匹配内容
* @param text 待爬取的文本
* @param regex 正则表达式
*/
private static void extractByRegex(String text, String regex) {
// 1. 编译正则表达式为Pattern对象(预编译提高效率)
Pattern pattern = Pattern.compile(regex);
// 2. 创建Matcher匹配器,关联待匹配的文本
Matcher matcher = pattern.matcher(text);
// 3. 循环查找所有匹配项
while (matcher.find()) {
// matcher.group() 获取当前匹配的完整内容
System.out.println(matcher.group());
}
}
}
文本构造:text
变量包含了5组用户信息(含手机号和邮箱),模拟真实爬取场景
正则表达式:
- 手机号:
1[3-9]\\d{9}
匹配1开头+第二位3-9+9位数字(覆盖国内主流运营商号段) - 邮箱:
[\\w.-]+@[\\w.-]+\\.\\w{2,6}
匹配常见邮箱格式(如xxx@xxx.com/cn等)
核心流程:
Pattern.compile(regex)
:将正则表达式编译为Pattern对象(预编译可重复使用,提高效率)Matcher matcher = pattern.matcher(text)
:创建匹配器并关联待匹配文本matcher.find()
:循环查找所有匹配项(返回是否找到下一个匹配)matcher.group()
:获取当前匹配的具体内容