Java String matches() 方法
示例
检查字符串是否匹配正则表达式
String regex = "cat|dog|fish";
System.out.println("cat".matches(regex));
System.out.println("dog".matches(regex));
System.out.println("catfish".matches(regex));
System.out.println("doggy bag".matches(regex));
定义和用法
matches()
方法在字符串中搜索与正则表达式的匹配,并返回匹配结果。
语法
public String matches(String regex)
参数值
参数 | 描述 |
---|---|
regex | 一个正则表达式 |
技术详情
返回 | 一个 boolean 值如果正则表达式精确匹配字符串,则返回 true 。如果它不匹配字符串,则返回 false 。 |
---|
❮ String Methods