无涯教程-Java 正则 - Pattern boolean matches函数

90 阅读1分钟

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

参考链接

www.learnfk.com/javaregex/j…