gpt4 book ai didi

java - Matcher.lookingAt() 和 find() 有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:29:03 24 4
gpt4 key购买 nike

我正在看 Java 正则表达式教程,标题本身就很清楚了。看起来 Matcher.lookingAt() 正在尝试匹配整个字符串。是真的吗?

最佳答案

documentation for Matcher.lookingAt清楚地解释了 lookingAt 尝试匹配的区域:

Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

所以不,lookingAt 不需要匹配整个字符串。那么lookingAtfind有什么区别呢?来自Matcher Javadoc overview :

  • The matches method attempts to match the entire input sequence against the pattern.
  • The lookingAt method attempts to match the input sequence, starting at the beginning, against the pattern.
  • The find method scans the input sequence looking for the next subsequence that matches the pattern.

lookingAt 总是从头开始,但是 find 会扫描起始位置。

从另一个角度看,matches 有一个固定的开始和结束,lookingAt 有一个固定的开始但一个可变的结束,find 有一个可变的开始和结束。

关于java - Matcher.lookingAt() 和 find() 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30008397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com