正则表达式是一种强大的字符串匹配工具,用于在文本中查找、替换和提取特定模式的字符串。它由一系列字符和特殊字符组成,可以表示字符串的模式。使用正则表达式可以方便地进行字符串的匹配、验证和处理。
正则表达式的模式由普通字符和特殊字符组成。普通字符表示自身,特殊字符用来表示一类字符或一段模式。
正则表达式 | 说明 |
\d | 匹配任何一个字母字符。示例:"a", "Z" |
\D | 匹配任何一个非数字字符。示例:"abc", "_" |
\w | 匹配任何一个字母字符。示例:"a", "Z" |
\W | 匹配任何一个非字母字符。示例:"1", "%" |
\s | 匹配任何一个空白字符,包括空格、制表符、换行符等" ", "\t", "\n" |
\S | 匹配任何一个非空白字符。 |
. | 匹配任何一个字符,包括字母、数字、特殊字符等。"a", "1", "%" |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
String text = "Hello, 123 world!";
// 匹配数字字符集合
Pattern pattern1 = Pattern.compile("\\d");
Matcher matcher1 = pattern1.matcher(text);
while (matcher1.find()) {
System.out.println("匹配到数字字符: " + matcher1.group());
}
// 匹配非数字字符集合
Pattern pattern2 = Pattern.compile("\\D");
Matcher matcher2 = pattern2.matcher(text);
while (matcher2.find()) {
System.out.println("匹配到非数字字符: " + matcher2.group());
}
// 匹配字母字符集合
Pattern pattern3 = Pattern.compile("\\w");
Matcher matcher3 = pattern3.matcher(text);
while (matcher3.find()) {
System.out.println("匹配到字母字符: " + matcher3.group());
}
// 匹配非字母字符集合
Pattern pattern4 = Pattern.compile("\\W");
Matcher matcher4 = pattern4.matcher(text);
while (matcher4.find()) {
System.out.println("匹配到非字母字符: " + matcher4.group());
}
// 匹配空白字符集合
Pattern pattern5 = Pattern.compile("\\s");
Matcher matcher5 = pattern5.matcher(text);
while (matcher5.find()) {
System.out.println("匹配到空白字符: " + matcher5.group());
}
// 匹配非空白字符集合
Pattern pattern6 = Pattern.compile("\\S");
Matcher matcher6 = pattern6.matcher(text);
while (matcher6.find()) {
System.out.println("匹配到非空白字符: " + matcher6.group());
}
// 匹配任意字符集合
Pattern pattern7 = Pattern.compile(".");
Matcher matcher7 = pattern7.matcher(text);
while (matcher7.find()) {
System.out.println("匹配到任意字符: " + matcher7.group());
}
}
}
正则表达式 | 说明 |
[abc] | 匹配单个字符,它可以是"a"、"b"或"c"中的任意一个 |
[a-z] | 匹配单个小写字母,它可以是字母表中的任何一个小写字母。 |
[abcABC] | 匹配单个字符,它可以是"a"、"b"、"c"、"A"、"B"或"C"中的任意一个。 |
[a-zA-Z0-9] | 匹配单个字符,它可以是字母(小写或大写)或数字中的任何一个。 |
[a-z&&[^bc]] | 匹配单个字符,它是一个小写字母,但不是"b"或"c"。 |
[^abc] | 匹配任何一个非a、b、c的任意字符。 |
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
String text = "abc123ABCxyz";
// [abc]
Pattern pattern1 = Pattern.compile("[abc]");
Matcher matcher1 = pattern1.matcher(text);
while (matcher1.find()) {
System.out.println("匹配到字符 [abc]: " + matcher1.group());
}
// [a-z]
Pattern pattern2 = Pattern.compile("[a-z]");
Matcher matcher2 = pattern2.matcher(text);
while (matcher2.find()) {
System.out.println("匹配到小写字母 [a-z]: " + matcher2.group());
}
// [abcABC]
Pattern pattern3 = Pattern.compile("[abcABC]");
Matcher matcher3 = pattern3.matcher(text);
while (matcher3.find()) {
System.out.println("匹配到字符 [abcABC]: " + matcher3.group());
}
// [a-zA-Z0-9]
Pattern pattern4 = Pattern.compile("[a-zA-Z0-9]");
Matcher matcher4 = pattern4.matcher(text);
while (matcher4.find()) {
System.out.println("匹配到字母或数字 [a-zA-Z0-9]: " + matcher4.group());
}
// [a-z&&[^bc]]
Pattern pattern5 = Pattern.compile("[a-z&&[^bc]]");
Matcher matcher5 = pattern5.matcher(text);
while (matcher5.find()) {
System.out.println("匹配到非b和c的小写字母 [a-z&&[^bc]]: " + matcher5.group());
}
// [^abc]
Pattern pattern6 = Pattern.compile("[^abc]");
Matcher matcher6 = pattern6.matcher(text);
while (matcher6.find()) {
System.out.println("匹配到非a、b、c的字符 [^abc]: " + matcher6.group());
}
}
}
正则表达式 | 说明 |
X? | 表示模式X出现0次或1次。问号?表示前面的字符或表达式在匹配中是可选的。 |
X* | 表示模式X出现0次或多次。星号 |
X+ | 表示模式X出现1次或多次。加号 |
X{n} | 表示模式X出现恰好n次。其中,n是一个非负整数。 |
X{n,} | 表示模式X出现至少n次。其中, |
X{n,m} | 表示模式X出现至少n次,但不超过m次。其中,n和m都是非负整数,并且n小于等于m |
import re
text = "XX XY XXX XXXY XXXX"
# X?
pattern1 = re.compile("X?")
matches1 = pattern1.findall(text)
print("X? 匹配结果:", matches1)
# X*
pattern2 = re.compile("X*")
matches2 = pattern2.findall(text)
print("X* 匹配结果:", matches2)
# X+
pattern3 = re.compile("X+")
matches3 = pattern3.findall(text)
print("X+ 匹配结果:", matches3)
# X{n}
pattern4 = re.compile("X{2}")
matches4 = pattern4.findall(text)
print("X{2} 匹配结果:", matches4)
# X{n,}
pattern5 = re.compile("X{2,}")
matches5 = pattern5.findall(text)
print("X{2,} 匹配结果:", matches5)
# X{n,m}
pattern6 = re.compile("X{2,3}")
matches6 = pattern6.findall(text)
print("X{2,3} 匹配结果:", matches6)
综合练习:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExercise {
public static void main(String[] args) {
// 要匹配的文本
String text = "这是一个示例文本,其中包含日期2024-03-25、电话号码(123)456-7890和邮箱example@example.com。";
// 匹配日期
Pattern datePattern = Pattern.compile("\\b\\d{4}-\\d{2}-\\d{2}\\b");
Matcher dateMatcher = datePattern.matcher(text);
while (dateMatcher.find()) {
System.out.println("匹配到日期: " + dateMatcher.group());
}
// 匹配电话号码
Pattern phonePattern = Pattern.compile("\\(\\d{3}\\)\\d{3}-\\d{4}");
Matcher phoneMatcher = phonePattern.matcher(text);
while (phoneMatcher.find()) {
System.out.println("匹配到电话号码: " + phoneMatcher.group());
}
// 匹配邮箱地址
Pattern emailPattern = Pattern.compile("\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b");
Matcher emailMatcher = emailPattern.matcher(text);
while (emailMatcher.find()) {
System.out.println("匹配到邮箱地址: " + emailMatcher.group());
}
}
}