正则表达式中的 Pattern.matches() 方法在 Java
java.util.regex.Pattern.matches() 方法匹配正则表达式和给定的输入。它有两个参数,即正则表达式和输入。如果正则表达式和输入匹配,则返回 true,否则返回 false。
一个演示 Java 正则表达式中 Pattern.matches() 方法的程序如下所示
示例
import java.util.regex.Pattern;
public class Demo {
public static void main(String args[]) {
String regex = "a*b";
String input = "aaab";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
boolean match = Pattern.matches(regex, input);
System.out.println("
Does the regex match with the input? " + match);
}
}输出
Regex: a*b Input: aaab Does the regex match with the input? true
现在让我们来理解一下上面的程序。
打印正则表达式和输入值。然后,Pattern.matches() 方法匹配正则表达式和给定的输入,并打印结果。以下代码片段演示了这一点
String regex = "a*b";
String input = "aaab";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
boolean match = Pattern.matches(regex, input);
System.out.println("
Does the regex match with the input? " + match);
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP