package cn.edu.bjut.textmining.chapter2; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexDemo { public static void main(String[] args) { String rawText = "联系人: 李四, 电话: 138-1234-5678, 邮箱: lisi@company.com; " + "联系人: 张三, 电话: 159-0000-1111, 邮箱: zhangsan@university.edu.cn"; extractInfo("手机号", "\\d{3}-\\d{4}-\\d{4}", rawText); extractInfo("邮箱", "[\\w\\.-]+@[\\w\\.-]+\\.[a-zA-Z]{2,}", rawText); } private static void extractInfo(String type, String regex, String content) { System.out.println("--- 提取 " + type + " ---"); Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); while (matcher.find()) { System.out.println("找到: " + matcher.group() + " (位置: " + matcher.start() + "-" + matcher.end() + ")"); } } }