正则表达式 (Regular Expression,简称 regex) 是一种强大的文本处理工具,可以用来匹配、搜索和替换文本中的特定模式。在 Java 中,正则表达式由 java.util.regex 包提供支持。
正则表达式使用特殊的字符和符号来定义匹配模式。一些常用的元字符如下:
. : 匹配任意单个字符
* : 匹配前面的字符零次或多次
+ : 匹配前面的字符一次或多次
? : 匹配前面的字符零次或一次
[] : 匹配括号内的任意单个字符
[^] : 匹配不包含在括号内的任意单个字符
^ : 匹配字符串开头
$ : 匹配字符串结尾
\d : 匹配任意数字
\w : 匹配任意字母、数字或下划线
\s: 匹配任意空白字符
Java 中使用 Pattern 类来编译正则表达式,并使用 Matcher 类来执行匹配操作。
代码示例:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
// 1. 定义正则表达式
String regex = "\\d+"; // 匹配一个或多个数字
// 2. 编译正则表达式
Pattern pattern = Pattern.compile(regex);
// 3. 创建 Matcher 对象
String text = "There are 123 apples and 456 oranges.";
Matcher matcher = pattern.matcher(text);
// 4. 查找匹配项
while (matcher.find()) {
// 5. 获取匹配的字符串
String matchedString = matcher.group();
System.out.println("Matched string: " + matchedString);
}
}
}代码解释:
Pattern.compile(regex) 将正则表达式字符串编译成 Pattern 对象。
matcher.find() 尝试在文本中找到下一个匹配项。
matcher.group() 返回当前匹配项的字符串。
输出结果:
Matched string: 123
Matched string: 456matcher.matches(): 检查整个文本是否完全匹配正则表达式。
matcher.lookingAt(): 检查文本开头是否匹配正则表达式。
matcher.replaceAll(replacement): 将所有匹配项替换为指定字符串。
matcher.replaceFirst(replacement): 将第一个匹配项替换为指定字符串。
代码示例:
String text = "Hello, world!";
String regex = "\\w+"; // 匹配一个或多个字母
// 替换所有匹配项为 "***"
String replacedText = matcher.replaceAll("***");
System.out.println(replacedText); // 输出: ***, ***!
// 替换第一个匹配项为 "Hi"
String replacedText2 = matcher.replaceFirst("Hi");
System.out.println(replacedText2); // 输出: Hi, world!提取电子邮件地址:
编写一个 Java 程序,使用正则表达式从以下文本中提取所有电子邮件地址:
"Contact us at support@example.com or info@example.org for any inquiries."
代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EmailExtractor {
public static void main(String[] args) {
String text = "Contact us at support@example.com or info@example.org for any inquiries.";
String regex = "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println("Email: " + matcher.group());
}
}
}验证电话号码格式:
编写一个 Java 程序,使用正则表达式验证以下电话号码是否符合格式:
123-456-7890
代码:
import java.util.regex.Pattern;
public class PhoneNumberValidator {
public static void main(String[] args) {
String phoneNumber = "123-456-7890";
String regex = "\\d{3}-\\d{3}-\\d{4}";
boolean isValid = Pattern.matches(regex, phoneNumber);
if (isValid) {
System.out.println("Valid phone number.");
} else {
System.out.println("Invalid phone number.");
}
}
}替换文本中的所有空格为下划线:
编写一个 Java 程序,使用正则表达式将以下文本中的所有空格替换为下划线:
"This is a test string."
代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SpaceReplacer {
public static void main(String[] args) {
String text = "This is a test string.";
String regex = "\\s";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
String replacedText = matcher.replaceAll("_");
System.out.println(replacedText); // 输出: This_is_a_test_string.
}
}原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。