I have 2 java programs that does a pattern matching,
Program - 1
public class test {
public static void main(String[] args) {
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("ab34ef");
boolean b = false;
while (b = m.find()){
System.out.println(m.start());
System.out.println(m.group());
}
}
}
Output:
0
1
2
34
4
5
6
Program - 2
public class test {
public static void main(String[] args) {
Pattern p = Pattern.compile("Dog");
Matcher m = p.matcher("This is a Dog and Dog name is Tommy");
boolean b = false;
while (b = m.find()){
System.out.println(m.start());
System.out.println(m.group());
}
}
}
Output-
10
Dog
18
Dog
Can someone explain how the regex works in both these cases.. Why in program-1 the matching is started from byte 0 and there on...whereas in program-2 the matching is matched on the whole string?