What is the difference between Matcher.lookingAt () and find ()?

I look at the Java regex tutorial, the title pretty much explains itself. It seems that Matcher.lookingAt () is trying to match the entire string. It's true?

+5
source share
2 answers

The documentation for Matcher.lookingAt clearly explains that the lookingAt area lookingAt trying to match:

Like the matches method, this method always starts at the beginning of the scope; unlike this method, it does not require coordination of the entire area.

So no, lookingAt does not require matching the entire string. Then what is the difference between lookingAt and find ? From the Matcher Javadoc Review :

  • The matches method tries to match the entire input sequence with a pattern.
  • The lookingAt method tries to match an input sequence, starting from the beginning, with a pattern.
  • The find method scans the input sequence, looking for the next subsequence that matches the pattern.

lookingAt always starts from the beginning, but find will scan the original position.

On the other hand, matches has a fixed start and end, lookingAt has a fixed start, but a variable end, and find has a variable start and end.

+17
source

lookingAt() always starts checking from the beginning and returns true when a match occurs.

find() can find several matches as it keeps the current position, like an iterator.

About find() from http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#find%28%29 :

This method starts at the beginning of this pairing area or, if the previous method call was successful, and the counter has not been reset since then, the first character does not match the previous match.

+1
source

All Articles