Java正则表达式基于java.util.regex包,核心类是Pattern和Matcher。基本语法遵循标准正则规范:
\d 数字 [0-9]
\D 非数字 [^0-9]
\s 空白字符 [ \t\n\x0B\f\r]
\S 非空白字符 [^\s]
\w 单词字符 [a-zA-Z_0-9]
\W 非单词字符 [^\w]
^ 匹配行首
$ 匹配行尾
b 匹配单词边界
B 匹配非单词边界
在Java字符串中需要使用双反斜杠转义:
// 匹配数字的正则表达式
String regex = "\d+"; // 实际表示 d+
import java.util.regex.*;
String text = "Hello 123 World";
// Pattern:表示正则表达式
Pattern pattern = Pattern.compile("\d+");
// Matcher:文本匹配器,从头开始读取,直到读取到匹配的字符串
Matcher matcher = pattern.matcher(text);
// 查找匹配
while (matcher.find()) {
System.out.println("Found: " + matcher.group());
}
// 匹配整个字符串
boolean isMatch = Pattern.matches("Hello.*", text);
String str = "aaaaaaaaaabbbbbbbbaaaaaaaaa";
System.out.println("--------- 贪婪匹配 ---------");
// 贪婪匹配
Pattern p = Pattern.compile("ab+");
Matcher m = p.matcher(str);
while (m.find()) {
System.out.println(m.group());
}
System.out.println("--------- 懒惰匹配 ---------");
// 懒惰匹配
Pattern p1 = Pattern.compile("ab+?");
Matcher m1 = p1.matcher(str);
while (m1.find()) {
System.out.println(m1.group());
}
效果图:
使用()创建捕获组:
String str = "a123a";
String str1 = "abc123abc";
String str2 = "1117891111";
String str3 = "aa7879a";
// 捕获分组
System.out.println("--------- 捕获分组 ---------");
String regex = "(.).+\1";
String regex1 = "(.+).+\1";
String regex2 = "((.)\2).+\1";
System.out.println(str.matches(regex));
System.out.println(str1.matches(regex1));
System.out.println(str2.matches(regex2));
System.out.println(str3.matches(regex2));
// 非捕获分组
System.out.println("--------- 非捕获分组 ---------");
String str4 = "我要学学变变变变撑撑撑撑撑";
str4 = str4.replaceAll("(.)\1+", "$1");// replaceAll() 方法用于把所有满足匹配的字符串替换成指定的字符串
System.out.println(str4);
效果图:
String emailRegex = "^[\w-_.+]*[\w-_.]@([\w]+\.)+[\w]+[\w]$";
String phoneRegex = "^1[3-9]\d{9}$";
String idCardRegex = "^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$";
String urlRegex = "^(https?://)?([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$";
通过Pattern的常量设置匹配模式:
// 不区分大小写匹配
Pattern.CASE_INSENSITIVE
// 多行模式(^和$匹配每行的开头和结尾)
Pattern.MULTILINE
// 示例:不区分大小写匹配
Pattern.compile("hello", Pattern.CASE_INSENSITIVE).matcher("Hello").find(); // true
使用正则表达式进行字符串替换:
String text = "a1b2c3";
String replaced = text.replaceAll("\d", "-"); // a-b-c-
预编译常用正则表达式:
private static final Pattern EMAIL_PATTERN = Pattern.compile(emailRegex);
参与评论
手机查看
返回顶部