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

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

技术教程推荐

算法面试通关40讲 -〔覃超〕

零基础学Java -〔臧萌〕

DDD实战课 -〔欧创新〕

Selenium自动化测试实战 -〔郭宏志〕

零基础入门Spark -〔吴磊〕

Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕

网络排查案例课 -〔杨胜辉〕

李智慧 · 高并发架构实战课 -〔李智慧〕

结构执行力 -〔李忠秋〕

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