File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regex Question from K&B book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regex Question from K&B book" Watch "Regex Question from K&B book" New topic

Regex Question from K&B book

salil kulkarni

Joined: Sep 18, 2007
Posts: 3

I found this question at:

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() +;

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.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Welcome to JavaRanch.

That questions gets asked so frequently that it has its own entry in the FAQ:
salil kulkarni

Joined: Sep 18, 2007
Posts: 3
Thanks Ulf, the answer just confirmed what I was suspecting but not sure about.
That explains it.
I agree. Here's the link:
subject: Regex Question from K&B book
It's not a secret anymore!