The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K & B book- chapter 6 - excercie1-not able to understand 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 "K & B book- chapter 6 - excercie1-not able to understand" Watch "K & B book- chapter 6 - excercie1-not able to understand" New topic

K & B book- chapter 6 - excercie1-not able to understand

m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
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?
A. 234
B. 334
C. 2334
D. 0123456
E. 01234456
F. 12334567
G. Compilation fails.
E is correct. The \d is looking for digits. The * is a quantifier that looks for 0 to many occurrences of the pattern that precedes it. Because we specified *, the group() method returns empty Strings until consecutive digits are found, so the only time group() returns a value is when it returns 34 when the matcher finds digits starting in position 2. The
start() method returns the starting position of the previous match because, again,we said find 0 to many occurrences.

my interpretation
0 1 2 3 4 5
a b 3 4 e f

so it can be - 0123445..there is no 6th index position.how 01234456 is possible. please explain
Christophe Verré

Joined: Nov 24, 2005
Posts: 14687

This FAQ may help.

[My Blog]
All roads lead to JavaRanch
Mamadou Touré
Ranch Hand

Joined: Dec 27, 2007
Posts: 189

They put the result like this to make it trickier . In fact the result you see is the concatenantion of the starting postion of each group (m.start() ) and the group itself( m.group()). In other words rewrite the system.out.println like this and you will see clearly the result
System.out.print("st--> " +m.start() + " gr-- >" + m.group());

that will give this as result
st--> 0 gr-- >st--> 1 gr-- >st--> 2 gr-- >34st--> 4 gr-- >st--> 5 gr-- >st--> 6 gr-- >

Hope this will help

SCJP 5 (76%)
SCWCD 5 (86%)
SCBCD 5(70%)
"The greatest glory in living lies not in never falling, but in raising every time we fall.".. Nelson Mandela
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
can you little elaborate. I AM NOT ABLE TO GET

Thanks<br />Dinesh
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41119
Did you read the FAQ entry Christophe pointed you to? What about it don't you understand?

Ping & DNS - my free Android networking tools app
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
The asterisk (*) is a "greedy quantifier," specifying that whatever preceeds it (in this case, any digit) should be matched zero or more times. By allowing for zero occurrances, a match of zero length is possible. Because a match of zero length is possible, the find() method will check the index following the last character of input.

Note that for a match of zero length, the matcher's start() and end() methods both return the same index. In these cases, the group() method returns an empty String (i.e., the substring from start() to end()).

I am not getting this . Can you explain in simple words to get me clear picture of this.
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
Is the zero length match confusing?

Let's say the pattern is "\d*" and the string to be matched against is "abcdef", how many matches will there be from beginning to end?

The first at start index 0, ending index 0, the match found is "". Because "" is zero digits, and that's what we were looking for (zero or more).

The second at start index 1, ending index 1, the match found is "". For the same reason as above, we found zero digits again. But we started one position further (or we'd be endlessly busy).

And so on.

The link posted by Christopher really helps.

Java hobbyist.
M Marsden

Joined: Jun 18, 2008
Posts: 18
I'm also confused here - when I run this example (using Netbeans in Windows), it finds nothing - should the argument be typed differently than as given? I tried adding some print lines before during and after the while loop, and it wasn't even going into the while. Netbeans has been acting pretty funny lately, but I tried this on two computers and both acted the same...

(I also tried compiling at the command line, and got slightly better results -


but that was not the expected 01234456 ...)
Juan Manuel Alberto de los Santos
Ranch Hand

Joined: Jun 26, 2008
Posts: 48
The Regular Expresions (regex) is a complicated topic, and it is very bad explained at K&B book ...

if you are studying for the SCJP exam, don't worry there will not be regex expressions in your exam, don't waste your time with that ... you just have to know what "d" means, what "s" means and so on ...

now if your intentions is to learn regex, i highly recommend you to buy a book specialized in regular expresions
M Marsden

Joined: Jun 18, 2008
Posts: 18
Well, I did finally get the right answer from Windows command line on one computer and from Netbeans under Linux on another - then it struck me, I should make sure it's getting the right arguments, and when I print out the arguments it got, it was a couple directories, instead of "/d*" and ab34ef ...

Now if anyone can explain that... !? (Realize I'm probably in the wrong Thread now, discussion-wise...)
Swathi Kota
Ranch Hand

Joined: Jun 04, 2008
Posts: 52
See this Sun tutorial..This is very good..

Sun tutorial for Regex

Success is how high you bounce when you hit bottom
Sazzad Hossain

Joined: Jul 21, 2008
Posts: 10
To Mamadou Tour�
Thanks a lot. The way you explained it make total sense to me.
M Marsden

Joined: Jun 18, 2008
Posts: 18
Well, the problem did appear to be a Netbeans one, so I moved discussion of that issue to a Netbeans Users forum (at Nabble):


Basically, I found a workaround to get the code working in Netbeans (add a space in front of the regex at command line, and trim in the code). It seems a little sloppy to me, but it works.

Anyway, I'm retaking the SCJP next week! Been studying like crazy, hope to pass this time. Good luck to anyone else in this boat!
I agree. Here's the link: http://aspose.com/file-tools
subject: K & B book- chapter 6 - excercie1-not able to understand
Similar Threads
can some one explain me the output of this program?
K&B Study Guide for Java 5 p498 Selftest problem 1
SCJP book regex, pattern and matcher question
Regex Doubt
Doubt in K&B Book.