Java正则表达式中matches()和find()之间的差异
我试图理解和之间的区别 根据Javadoc(据我所知),Java正则表达式中matches()和find()之间的差异,java,regex,Java,Regex,我试图理解和之间的区别 根据Javadoc(据我所知),matches()将搜索整个字符串,即使它找到了它要查找的内容,find()将在找到它要查找的内容时停止 如果这个假设是正确的,我就看不出您何时想使用matches()而不是find(),除非您想计算它找到的匹配数 在我看来,String类应该有find()而不是matches()作为内置方法 因此,总结一下: 我的假设正确吗 什么时候使用matches()而不是find()有用 匹配如果整个字符串与给定模式匹配,则返回truefind尝试
matches()
将搜索整个字符串,即使它找到了它要查找的内容,find()
将在找到它要查找的内容时停止
如果这个假设是正确的,我就看不出您何时想使用matches()
而不是find()
,除非您想计算它找到的匹配数
在我看来,String类应该有find()
而不是matches()
作为内置方法
因此,总结一下:
matches()
而不是find()
有用匹配
如果整个字符串与给定模式匹配,则返回truefind
尝试查找与模式匹配的子字符串。matches
尝试将表达式与整个字符串匹配,并在模式的开头和结尾隐式添加一个^
,这意味着它不会查找子字符串。因此,该代码的输出:
public static void main(String[] args) throws ParseException {
Pattern p = Pattern.compile("\\d\\d\\d");
Matcher m = p.matcher("a123b");
System.out.println(m.find());
System.out.println(m.matches());
p = Pattern.compile("^\\d\\d\\d$");
m = p.matcher("123");
System.out.println(m.find());
System.out.println(m.matches());
}
/* output:
true
false
true
true
*/
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d");
String candidate = "Java123";
Matcher m = p.matcher(candidate);
if (m != null){
System.out.println(m.find());//true
System.out.println(m.matches());//false
}
}
123
是a123b
的子字符串,因此find()
方法输出truematches()
仅“查看”a123b,它与123
不同,因此输出false。matches()
仅在匹配完整字符串时返回true。
find()
将尝试查找与正则表达式匹配的子字符串中的下一个。注意强调“下一步”。这意味着多次调用find()
的结果可能不同。此外,通过使用find()
可以调用start()
返回子字符串匹配的位置
final Matcher subMatcher = Pattern.compile("\\d+").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + subMatcher.matches());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find() + " - position " + subMatcher.start());
System.out.println("Found: " + subMatcher.find());
System.out.println("Found: " + subMatcher.find());
System.out.println("Matched: " + subMatcher.matches());
System.out.println("-----------");
final Matcher fullMatcher = Pattern.compile("^\\w+$").matcher("skrf35kesruytfkwu4ty7sdfs");
System.out.println("Found: " + fullMatcher.find() + " - position " + fullMatcher.start());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Found: " + fullMatcher.find());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
System.out.println("Matched: " + fullMatcher.matches());
将输出:
Found: false
Found: true - position 4
Found: true - position 17
Found: true - position 20
Found: false
Found: false
Matched: false
-----------
Found: true - position 0
Found: false
Found: false
Matched: true
Matched: true
Matched: true
Matched: true
发现:错误
发现:正确-位置4
发现:正确-位置17
发现:正确-位置20
发现:错误
发现:错误
匹配:假
-----------
找到:true-位置0
发现:错误
发现:错误
匹配:正确
匹配:正确
匹配:正确
匹配:正确
因此,如果未重置Matcher
对象,则多次调用find()
时要小心,即使正则表达式被^
和$
包围以匹配完整字符串。matches()代码>不缓冲,但find()
缓冲find()
首先搜索字符串的末尾,为结果编制索引,然后返回布尔值和相应的索引
这就是为什么当你有一个像
1:Pattern.compile("[a-z]");
2:Pattern.matcher("0a1b1c3d4");
3:int count = 0;
4:while(matcher.find()){
5:count++: }
在4:使用模式结构的正则表达式引擎将读取整个代码(索引到索引由regex[单个字符]指定)
查找至少一个匹配项。如果找到了此类匹配项,则将根据索引结果对其进行索引,否则,如果循环没有进行预先计算,则循环将根据索引结果执行,而matches()
;则不会执行。while语句将永远不会执行,因为匹配字符串的第一个字符不是字母表。find()将考虑正则表达式中的子字符串,其中“<代码>匹配())/代码>将考虑完整表达式。
find()
仅当表达式的子字符串与模式匹配时才会返回true
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d");
String candidate = "Java123";
Matcher m = p.matcher(candidate);
if (m != null){
System.out.println(m.find());//true
System.out.println(m.matches());//false
}
}
你可以说matches(p)
与find(“^”+p+“$”)
相同,如果这更清楚的话。举个例子来澄清答案:“[a-z]+”和字符串“123abc123”将使用matches()失败,但使用find()将成功。@Max确切地说,“123abc123”。匹配([a-z]+”
将与“123abc123.find(^[a-z]+”)一样失败
会的。我的观点是,matches()
是完全匹配的,就像find()
具有起始和结束锚一样。Pattern.compile(“某些模式”).matcher(str).matcher(
等于Pattern.compile(“^some Pattern$”).matcher(str.find()
@AlexR/@jensgram:…(“某些模式”).matcher(str).matches()
与…(^some pattern$).matcher(str).find()
仅在第一次调用中是正确的。请参阅下面的我的答案。请注意,多次调用find()
可能会为同一matcher
返回不同的结果。请参阅下面的我的答案。这个答案有误导性。matchers()
不仅仅是一个包含“^”和“$”的find()
。请注意,如果没有reset()
,多次调用.find()
可能会产生不同的结果,而matches()
将始终返回相同的结果。请参阅下面我的答案。非常有用的朋友