Java正则表达式中的Pattern.matches()方法
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("\nDoes 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("\nDoes the regex match with the input? " + match);热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短