Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex SCJP 5.0

 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

import java.util.regex.*;
public class Regex2 {
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.print(m.start() + m.group());
}

}
}
Output : 01234456

Question from "SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) by Kathy SierraandBert Bates" Chapter 6: String, I/O, Formatting, Parsing, Self Test : Question 1.

I don't understand why the index can reaches 6? As I know, m.start() start from 0 then 1,2, matches 34, after that index become 4,5,6 i don't understand. Please help.
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Edmen,

have a look at this posting from several weeks ago: quantifier.

Regards,
Thomas

[ October 23, 2008: Message edited by: Thomas Thevis ]
[ October 23, 2008: Message edited by: Thomas Thevis ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic