matches() 方法用于检测字符串是否匹配给定的正则表达式。
调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:
package com.example.面向对象1;
import java.util.*;
import java.util.regex.Pattern;
public class listdemo2 {
public static void main(String[] args) {
//demo1
String content = "I am coder " +
"from nowcoder.com.";
String pattern = ".*nowcoder.*";
//content中是否包含pattern的内容呢
boolean isMatch = Pattern.matches(pattern, content);
System.out.println("字符串中是否包含了 'nowcoder' 子字符串? " + isMatch);
//demo2 获取前后存在数据则用(.*) 不存则如第3个 直接写即可
String Str = new String("www.runoob.com");
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)runoob(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)google(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("www(.*)"));
}
}