Why does adding an if statement before the while loop result in the first match being removed?
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class testRegex {
public static void main(String[]args) {
Pattern p = Pattern.compile("[a-zA-Z0-9]+");
Matcher m = p.matcher("Name, 245 Street, City, Country, 101010");
System.out.println(m.find());
if(m.find()) {
while(m.find()) {
System.out.println(m.group());
}
} else {
System.out.println("Nothing found");
}
}
}
The output without the if is correct.
Name
245
Street
City
Country
101010
Adding the if or anything before the while loop for that matter results in this:
245
Street
City
Country
101010
What am I doing wrong and how can I correct this? I'm doing this on Eclipse IDE.
iffind()"attempts to find the next subsequence of the input sequence that matches the pattern", so when you haveprintlnorifbeforewhile, they "consume" one or severalfind()iterations.not reproducible or was caused by typos?