一、方法定义

public boolean matches(String regex)
  • 所属类java.lang.String
  • 功能:判断整个字符串是否完全匹配指定的正则表达式。
  • 参数
    • regex:正则表达式(String类型)。
  • 返回值
    • true:如果字符串完全匹配正则表达式。
    • false:如果字符串不匹配或抛出异常。
  • 异常
    • PatternSyntaxException:如果正则表达式语法无效。

二、核心功能

  1. 全字符串匹配
    • matches()要求整个字符串完全匹配正则表达式。
    • 例如:"Hello".matches("H.*o")返回true,因为H.*o匹配从H开始、以o结束的任意长度字符串。
  2. 区分大小写
    • 默认区分大小写,需通过正则表达式标志符(如(?i))调整。
  3. 支持复杂模式
    • 支持正则表达式的全部功能(如分组、量词、字符类等)。

三、使用示例

1. 基础用法

String str = "Java Programming";
boolean result1 = str.matches("Java.*"); // true(匹配以Java开头的任意字符串)
boolean result2 = str.matches("Java");    // false(字符串长度不匹配)
System.out.println(result1); // 输出: true
System.out.println(result2); // 输出: false

2. 验证手机号码

String phoneNumber = "13800138000";
String regex = "1[345789]\\d{9}"; // 匹配中国大陆手机号
boolean isValid = phoneNumber.matches(regex);
System.out.println("手机号是否有效: " + isValid); // 输出: true

3. 忽略大小写的匹配

String str = "HELLO";
String regex = "(?i)hello"; // (?i)表示忽略大小写
boolean isMatch = str.matches(regex);
System.out.println("是否匹配(忽略大小写): " + isMatch); // 输出: true

4. 邮箱地址验证

String email = "user@example.com";
String regex = "\\w+@\\w+\\.\\w+"; // 简单邮箱格式
boolean isEmail = email.matches(regex);
System.out.println("邮箱是否有效: " + isEmail); // 输出: true

四、注意事项

  1. 全字符串匹配
    • matches()要求整个字符串匹配正则表达式,而非部分匹配。
    • 如果需查找子串匹配,应使用PatternMatcherfind()方法。
  2. 转义字符
    • 正则表达式中的特殊字符(如.*\)需在Java字符串中转义。
    • 例如:\d需写成\\d^需写成\\^
  3. 性能优化
    • 若需多次匹配同一正则表达式,建议使用Pattern.compile()预编译正则表达式。
    • 示例:
      Pattern pattern = Pattern.compile("1[345789]\\d{9}");
      Matcher matcher = pattern.matcher(phoneNumber);
      boolean isValid = matcher.matches();
      

五、常见错误

  1. 未转义特殊字符

    String str = "C:\\Users\\Test";
    boolean isMatch = str.matches("C:\\Users\\Test"); // 错误!需转义反斜杠
    // 正确写法:
    boolean isMatch = str.matches("C:\\\\Users\\\\Test");
    
  2. 误用matches()代替find()

    String str = "abc123def";
    boolean isMatch = str.matches("\\d+"); // false(整个字符串不是数字)
    // 正确方式:查找子串匹配
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(str);
    boolean found = matcher.find(); // true
    
  3. 正则表达式语法错误

    String regex = "[A-Z"; // 缺少闭合的字符类
    boolean isMatch = str.matches(regex); // 抛出 PatternSyntaxException
    

六、最佳实践

  1. 预编译正则表达式

    Pattern pattern = Pattern.compile("^[A-Za-z0-9]+$"); // 预编译
    Matcher matcher = pattern.matcher(input);
    if (matcher.matches()) { ... }
    
  2. 处理大小写不敏感

    String regex = "(?i)java"; // 忽略大小写
    boolean isMatch = str.matches(regex);
    
  3. 边界检查

    • 使用^$锚定字符串起始和结束位置,确保严格匹配。
    • 例如:^[A-Z][a-z]+$匹配以大写字母开头、后跟小写字母的字符串。
  4. 避免过度依赖matches()

    • 对于复杂匹配需求(如提取子串),使用Matchergroup()方法更灵活。

七、性能优化

场景 优化建议
高频调用 预编译正则表达式(Pattern.compile())以减少重复编译开销。
复杂匹配 使用PatternMatcher分离编译和匹配逻辑。
简单校验 matches()足够高效,适合轻量级校验(如邮箱、电话号码)。

八、总结

  1. 核心功能matches()用于判断字符串是否完全匹配指定的正则表达式。
  2. 关键实践
    • 注意转义字符和正则表达式语法。
    • 区分全字符串匹配与子串匹配的场景。
    • 预编译正则表达式以提高性能。
  3. 典型应用
    • 数据校验(如邮箱、电话号码、密码格式)。
    • 简单文本过滤(如检查字符串是否符合特定规则)。