import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UserPattern {
/**
* @param args
*/
public static void main(String[] args) {
String phones1 = "justin 的手机号:0939-100391\n"
+"bush 的手机号:0939-668899\n";
Pattern pattern = Pattern.compile(".0939-\\d{6}");
Matcher matcher = pattern.matcher(phones1);
System.out.println("整体是否匹配:"+matcher.matches());
while (matcher.find()) {
System.out.println("\t 匹配的结果"+matcher.group());
System.out.println("匹配的开始索引"+matcher.start());
System.out.println("匹配的结束索引"+matcher.end());
}
}
}
本文通过一个Java程序示例介绍了如何使用正则表达式来查找特定的电话号码格式。该程序演示了Pattern和Matcher类的基本用法,并展示了如何进行匹配及获取匹配结果的开始和结束索引。
315

被折叠的 条评论
为什么被折叠?



