Matches in Java supports the index of found groups on a given line.
For example, the line shown in your example is BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E
There are 3 groups matching the pattern.
BIP1111E BIP1234E BIP5555E
When creating a template, it starts at index 0. When we iterate using m.find (), every time it finds a template, it marks the index position of the found template.
For example, the first gourp is at the beginning of a line, that is, it starts at 0 and continues to the 7th (0-based index) character of the line. The next time we say find (), it starts with the 8th character to find the next pattern match.
m.matches tries to match the entire string, and also manipulates the internal index.
when you call m.matches () before iterating using m.find (), the index moves from the initial 0. therefore, the first group of BIP1111E is skipped if you call m.matches ()
source share