• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex Question from K&B book

 
salil kulkarni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I found this question at:
http://www.wickedlysmart.com/SCJPStudyGuide/Java_5_SCJPquestions.html

and it says:

import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}

And the command line:

java Regex2 "\d*" ab34ef

What is the result?

The answer is: E). 01234456

As per my understanding, I am still confused as to how the '6' is derived in the above answer?

It would be great if someone can explain.

Thanks,
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

That questions gets asked so frequently that it has its own entry in the FAQ: http://faq.javaranch.com/java/ScjpFaq#kb-regexp
 
salil kulkarni
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf, the answer just confirmed what I was suspecting but not sure about.
That explains it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic