目录
一、正则表达式基础
正则表达式是一种用于匹配字符串的强大工具。它使用特定的语法来定义匹配模式,可以在文本处理、表单验证、数据提取等场景中发挥重要作用。
(一)元字符
元字符是正则表达式中的特殊字符,具有特殊含义。常见的元字符包括:
.
:匹配除换行符以外的任意单个字符。^
:匹配字符串的开始位置。$
:匹配字符串的结束位置。*
:匹配前面的子表达式零次或多次。+
:匹配前面的子表达式一次或多次。?
:匹配前面的子表达式零次或一次。{n}
:匹配前面的子表达式恰好 n 次。{n,m}
:匹配前面的子表达式至少 n 次,至多 m 次。[]
:定义一个字符集合,匹配其中的任意一个字符。|
:匹配左边或右边的表达式。()
:捕获括号内的表达式,形成一个分组。
(二)字符集
字符集用于定义一组字符,匹配其中的任意一个字符。常见的字符集包括:
[abc]
:匹配 a、b 或 c。[a-z]
:匹配小写字母 a 到 z。[A-Z]
:匹配大写字母 A 到 Z。[0-9]
:匹配数字 0 到 9。[a-zA-Z0-9]
:匹配字母或数字。
(三)量词
量词用于指定前面的字符或子表达式出现的次数。常见的量词包括:
*
:零次或多次。+
:一次或多次。?
:零次或一次。{n}
:恰好 n 次。{n,m}
:至少 n 次,至多 m 次。
二、正则表达式常用示例
(一)验证邮箱格式
邮箱格式通常由本地部分、@ 符号和域名部分组成。本地部分可以包含字母、数字、下划线、点和短横线,域名部分可以包含字母、数字、点和短横线。
public class EmailValidation {
public static void main(String[] args) {
String regex = "^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
String email1 = "test@example.com";
String email2 = "invalid_email";
System.out.println(email1.matches(regex)); // 输出:true
System.out.println(email2.matches(regex)); // 输出:false
}
}
(二)验证电话号码格式
电话号码格式可能因国家和地区而异。以下示例验证一个简单的电话号码格式,例如:123-456-7890。
public class PhoneNumberValidation {
public static void main(String[] args) {
String regex = "^\\d{3}-\\d{3}-\\d{4}$";
String phone1 = "123-456-7890";
String phone2 = "1234567890";
System.out.println(phone1.matches(regex)); // 输出:true
System.out.println(phone2.matches(regex)); // 输出:false
}
}
(三)提取网页中的链接
在网页中,链接通常以 <a>
标签的形式出现,href 属性包含链接的 URL。
import java.util.regex.*;
public class LinkExtractor {
public static void main(String[] args) {
String html = "<html><body><a href='https://example.com'>Example</a></body></html>";
String regex = "href=['\"](.*?)['\"]";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(html);
while (matcher.find()) {
System.out.println(matcher.group(1)); // 输出:https://example.com
}
}
}
(四)验证日期格式
日期格式通常为 YYYY-MM-DD 或 MM/DD/YYYY 等。以下示例验证 YYYY-MM-DD 格式。
public class DateValidation {
public static void main(String[] args) {
String regex = "^\\d{4}-\\d{2}-\\d{2}$";
String date1 = "2023-10-11";
String date2 = "10/11/2023";
System.out.println(date1.matches(regex)); // 输出:true
System.out.println(date2.matches(regex)); // 输出:false
}
}
(五)验证URL格式
URL 格式通常包括协议、域名和路径等部分。以下示例验证一个简单的 URL 格式。
public class URLValidation {
public static void main(String[] args) {
String regex = "^(http|https)://[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}(/[^\\s]*)?$";
String url1 = "https://example.com";
String url2 = "ftp://example.com";
System.out.println(url1.matches(regex)); // 输出:true
System.out.println(url2.matches(regex)); // 输出:false
}
}
三、正则表达式在Java中的应用
在 Java 中,可以使用 java.util.regex
包中的 Pattern
和 Matcher
类来处理正则表达式。
(一)匹配操作
import java.util.regex.*;
public class RegexMatch {
public static void main(String[] args) {
String text = "Hello, World!";
String regex = "World";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println("匹配成功!");
} else {
System.out.println("匹配失败!");
}
}
}
(二)替换操作
import java.util.regex.*;
public class RegexReplace {
public static void main(String[] args) {
String text = "Hello, World!";
String regex = "World";
String replacement = "Java";
String result = text.replaceAll(regex, replacement);
System.out.println(result); // 输出:Hello, Java!
}
}
(三)分割操作
import java.util.regex.*;
public class RegexSplit {
public static void main(String[] args) {
String text = "apple,banana,cherry";
String regex = ",";
String[] result = text.split(regex);
for (String s : result) {
System.out.println(s);
}
// 输出:
// apple
// banana
// cherry
}
}
四、总结
正则表达式是一种强大的文本处理工具,通过使用特定的语法可以定义复杂的匹配模式。在 Java 中,可以使用 Pattern
和 Matcher
类来处理正则表达式,实现匹配、替换和分割等操作。掌握正则表达式的基本语法和常用示例,可以大大提高我们在文本处理和数据验证方面的开发效率。希望本文的讲解和示例对您有所帮助,如果您在使用正则表达式时有任何疑问,欢迎随时交流探讨!