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));
   }
}

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

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

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

技术教程推荐

AI技术内参 -〔洪亮劼〕

软件测试52讲 -〔茹炳晟〕

Android开发高手课 -〔张绍文〕

10x程序员工作法 -〔郑晔〕

Go 并发编程实战课 -〔晁岳攀(鸟窝)〕

物联网开发实战 -〔郭朝斌〕

基于人因的用户体验设计课 -〔刘石〕

操作系统实战45讲 -〔彭东〕

AI 应用实战课 -〔黄佳〕

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