Why does Java regex “matches” vs “find” get a different match when using non-greedy pattern?

后端 未结 2 1342
终归单人心
终归单人心 2021-01-12 23:03

So I ran into a bug caused by expecting the matches() method to find exactly the same match as using find(). Normally this is the case, but it appears that if a non-greedy p

2条回答
  •  别那么骄傲
    2021-01-12 23:18

    This makes perfect sense.

    The matches(...) method must attempt to consume the whole string, so it does, even with a non-greedy pattern.

    The find(...) method may find a substring, so it stops at the point if finds any matching substring.

提交回复
热议问题