Java matches() 方法

138 阅读1分钟

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.*";

        boolean isMatch = Pattern.matches(pattern, content);
        System.out.println("字符串中是否包含了 'nowcoder' 子字符串? " + isMatch);
        //demo2
        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(.*)"));

    }


}

在这里插入图片描述