[英]Regex not finding match - difference between .find() and .matches() at fault?
[英]Difference between matches() and find() in Java Regex
matches
嘗試將表達式與整個字符串進行匹配,並在模式的開頭隱式添加^
並在模式的末尾添加$
,這意味着它不會查找 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ。 因此,此代碼的 output :
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
*/
123
是 a123b 的a123b
,因此find()
方法輸出 true。 matches()
僅“看到” a123b
,它與123
不同,因此輸出 false。
如果整個字符串與給定的模式匹配, matches
將返回 true。 find
嘗試查找與該模式匹配的 substring。
matches()
- 僅在匹配完整字符串時才返回truefind()
- 將嘗試在 substring 中查找與正則表達式匹配的下一個匹配項。 請注意在find()
的情況下強調“下一個”。 這意味着,多次調用find()
的結果可能不一樣。 此外,通過使用find()
您可以調用start()
以返回 position 與 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 匹配。
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
因此,如果Matcher
object 未重置,則在多次調用find()
時要小心,即使正則表達式被^
和$
包圍以匹配完整字符串也是如此。
find()
將根據正則表達式考慮子字符串,而matches()
將考慮完整表達式。
僅當表達式的子字符串與模式匹配時, 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();
不緩沖,但find()
緩沖。 find()
首先搜索到字符串的末尾,對結果進行索引,並返回 boolean 值和對應的索引。
這就是為什么當你有這樣的代碼時
1:Pattern.compile("[a-z]");
2:Pattern.matcher("0a1b1c3d4");
3:int count = 0;
4:while(matcher.find()){
5:count++: }
4:使用模式結構的正則表達式引擎將讀取整個代碼(由regex[single character]
指定的索引到索引以找到至少一個匹配項。如果找到這樣的匹配項,它將被索引,然后循環將根據索引結果執行 else 如果它沒有像 which matches()
; 沒有進行預先計算。while 語句將永遠不會執行,因為匹配字符串的第一個字符不是字母表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.