为什么要在Java正则表达式中使用整个字符串
在Java中,正则表达式matches()将输入字符串与整个字符串进行匹配,因为它在输入字符串的末尾添加了^和$,因此它将不匹配子字符串。因此,要匹配子字符串,应使用find()。
示例
import java.util.regex.*;
class PatternMatchingExample {
public static void main(String args[]) {
String content = "aabbcc";
String string = "aa";
Pattern p = Pattern.compile(string);
Matcher m = p.matcher(content);
System.out.println(" 'aa' Match:"+ m.matches());
System.out.println(" 'aa' Match:"+ m.find());
}
}输出结果
'aa' Match:false 'aa' Match:true
热门推荐
10 小学毕业父母简短祝福语
11 幼儿生日祝福语模板简短
12 新娘妈妈的祝福语简短
13 邻居聚会祝福语大全简短
14 十条祝福语简短
15 给同学祝福语的简短
16 过年祝福语简短祝妈妈
17 祝女儿简短祝福语大全
18 恩师诗词祝福语简短大全