为什么 lookingAt return 对 lookArounds 是 true 而 matches return false
Why does lookingAt return true for lookArounds while matches return false
我开始使用正则表达式进行环视
我认为正则表达式是 q 后跟 u 的正前瞻。但是当输入字符串为 "qu" 时,它不匹配。但是我得到了 lookingAt 函数的真实结果。
String regex="q(?=u)";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());
谁能解释为什么会这样?
我假设这是因为它试图匹配整个字符串
matches()
Attempts to match the entire region against the pattern.
从积极的角度来看,我认为它与 u
不匹配。
即匹配将是 q
如果 它后面是 u
。因此 q
不是整个测试字符串,也没有匹配整个字符串。
这就是为什么你可以写这个来得到 true
String regex="^q(?=u)u";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());
我开始使用正则表达式进行环视
我认为正则表达式是 q 后跟 u 的正前瞻。但是当输入字符串为 "qu" 时,它不匹配。但是我得到了 lookingAt 函数的真实结果。
String regex="q(?=u)";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());
谁能解释为什么会这样?
我假设这是因为它试图匹配整个字符串
matches()
Attempts to match the entire region against the pattern.
从积极的角度来看,我认为它与 u
不匹配。
即匹配将是 q
如果 它后面是 u
。因此 q
不是整个测试字符串,也没有匹配整个字符串。
这就是为什么你可以写这个来得到 true
String regex="^q(?=u)u";
Pattern p= Pattern.compile(regex);
String test = "qu";
Matcher m= p.matcher(test);
System.out.println(m.matches());
System.out.println(m.lookingAt());