无涯教程-Java - boolean matches(String regex)函数

88 阅读1分钟

此字符串是否与给定的正则表达式匹配,以str.matches(regex)形式调用此方法所产生的输出与表达式Pattern.matches(regex,str)完全相同。

 boolean matches - 语法

这是此方法的语法-

public boolean matches(String regex)

这是参数的详细信息-

  • regex   -  此字符串要匹配的正则表达式。

boolean matches - 返回值

  • 仅当此字符串与给定的正则表达式匹配时,此方法返回true。

boolean matches - 示例

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str=new String("Welcome to Learnfk.com");

      System.out.print("返回值 :" );
      System.out.println(Str.matches("(.*)Learnfk(.*)"));

      System.out.print("返回值 :" );
      System.out.println(Str.matches("LearnFk"));

      System.out.print("返回值 :" );
      System.out.println(Str.matches("Welcome(.*)"));
   }
}

这将产生以下输出-

返回值 :true
返回值 :false
返回值 :true

参考链接

www.learnfk.com/java/java-s…