Java 正则 中的 Pattern boolean match

首页 / 正则入门教程 / Java 正则 中的 Pattern boolean match

java.util.regex.Pattern.matches(String regex,CharSequence input)方法编译给定的正则表达式,并尝试将给定的表达式与之匹配。

static boolean matches - 声明

public static boolean matches(String regex, CharSequence input)
  • regex     -  要编译的表达式。

  • input     -  要匹配的字符序列。

static boolean matches - 异常

  • PatternSyntaxException   -  如果表达式的语法无效。

static boolean matches - 示例

下面的示例显示java.util.regex.Pattern.matches(String regex,CharSequence input)方法的用法。

package com.learnfk;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
   private static final String REGEX = "foo*";
   private static final String INPUT = "fooooooooooooooooo";

   public static void main( String args[] ) {
      System.out.println("Current REGEX is: "+REGEX);
      System.out.println("Current INPUT is: "+INPUT);
      System.out.println("matches(): "+Pattern.matches(REGEX,INPUT));
   }
}

让无涯教程编译并运行以上程序,这将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/javaregex/javaregex-pattern-matches.html

来源:LearnFk无涯教程网

Current REGEX is: foo*
Current INPUT is: fooooooooooooooooo
matches(): true

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Web协议详解与抓包实战 -〔陶辉〕

编辑训练营 -〔总编室〕

Spring Boot与Kubernetes云原生微服务实践 -〔杨波〕

DevOps实战笔记 -〔石雪峰〕

.NET Core开发实战 -〔肖伟宇〕

后端存储实战课 -〔李玥〕

如何落地业务建模 -〔徐昊〕

Web漏洞挖掘实战 -〔王昊天〕

AI大模型企业应用实战 -〔蔡超〕

好记忆不如烂笔头。留下您的足迹吧 :)