失效链接处理 |
Java 正则表达式的应用?qing)其实?- ZPattern与Matchercȝ邮g和电(sh)话匹?PDF 下蝲
相关截图Q?/strong>
![]() 主要内容Q?/strong>
?nbsp;Java 中,正则表达式(Regular ExpressionsQ通常?/span>
q?nbsp;java.util.regex包中?nbsp;Pattern?nbsp;MatchercL使用?/span>
下面是一个简单的例子Q展CZ(jin)如何使用正则表达式来?/span>
配和提取字符串中的特定部分?/span>
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static void main(String[] args) {
// 要匹配的字符?/em>
String text = "Hello, my email is example@example.
com and my phone number is 123-456-7890.";
// 定义正则表达式来匚w?sh)子邮g地址
String emailRegex = "\\b[A-Za-z0-9._%+-]+@[A-Za-z
0-9.-]+\\.[A-Z|a-z]{2,}\\b";
// 定义正则表达式来匚w?sh)话L(fng)
String phoneRegex = "\\b\\d{3}-\\d{3}-\\d{4}\\b";
// ~译正则表达?/em>
Pattern emailPattern = Pattern.compile(emailRegex)
;
Pattern phonePattern = Pattern.compile(phoneRegex)
;
// 创徏 Matcher 对象Matcher emailMatcher = emailPattern.matcher(text);
Matcher phoneMatcher = phonePattern.matcher(text);
// 查找?sh)子邮g地址
System.out.println("Email addresses found:");
while (emailMatcher.find()) {
System.out.println(emailMatcher.group());
}
// 查找?sh)话L(fng)
System.out.println("\nPhone numbers found:");
while (phoneMatcher.find()) {
System.out.println(phoneMatcher.group());
}
}
}
|