Makhlo
Makhlo

Reputation: 45

Regular Expression: Not all matches are printed when using System.out.println(m.matches());

I execute the following code:

public static void test() {
 Pattern p = Pattern.compile("BIP[0-9]{4}E");

 Matcher m = p.matcher("BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E");
 System.out.println(m.matches());
 while(m.find()) {
  System.out.println(m.group());
 }

}

What i cannot explain is when the code is executed with System.out.println(m.matches()); the matches printed are: BIP1234E and BIP5555E. but when System.out.println(m.matches()); is removed from code the matche BIP1111E is also printed.

Can someone please explain how that's possible ? Thnx a lot for your help.

Upvotes: 4

Views: 221

Answers (2)

khachik
khachik

Reputation: 28703

You can use Matcher.reset method to reset the matcher to its initial state after calling matches. That method changes the current state of the matcher object, and on the next call of find it starts looking after the first g character.

Upvotes: 0

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17235

Matcher in Java maintains an index of found groups in the given string.

For example in the string provided in you example - BIP1111EgjgjgjhgjhgjgjgjgjhgjBIP1234EfghfhfghfghfghBIP5555E

There are 3 groups matching the pattern

BIP1111E BIP1234E BIP5555E

When matcher is created it starts from index 0. When we iterate over the matcher using m.find(), every time it finds a pattern it marks the index position of the found pattern.

For example the first gourp is at start of the string - that is it starts at 0 and goes till 7th (0 based index) character of the string. Next time we say find() it starts from 8th character to find next match of pattern.

m.matches tries to match the whole string and it also manipulates the internal index.

when you call m.matches() before iterating using m.find() the index is moved from the initial 0. so the first group of BIP1111E is skipped if you call m.matches()

Upvotes: 1

Related Questions