• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Chapter 6 Self Test Q1

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Answer is E but i am not getting this please explain me.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check this out FAQ
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pattern "\d*" will look for 0 to many digits in the String.

The String given in the question is ab34ef

AS you must be aware of,Whenever the JVM finds the start() method it will display the starting index of the matching digit and whenever it finds group() method it will display the corresponding digit(s) found in the starting index.

In this particular case, the matching occurs as follows:

m.start() m.group()
--------------------------------------
0 no matching digit(s) satisfying "\d*"
1 no matching digit(s) satisfying "\d*"
2 34
3 already consumed in the previous match for "\d*"
4 no matching digit(s) satisfying "\d*"
5 no matching digit(s) satisfying "\d*"
6 no matching digit(s) satisfying "\d*"

combining this in m.start() + m.group for all the iterations we get as

01234456
[ May 09, 2008: Message edited by: Thirugnanam Saravanan ]
 
sapana jain
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for helping me.Now the whole concept is clear to me.Question are really very tough.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, what I don't get is where index 6 has come from:

0 no matching digit(s) satisfying "\d*" (a)
1 no matching digit(s) satisfying "\d*" (b)
2 34 (34)
3 (already consumed)
4 no matching digit(s) satisfying "\d*" (e)
5 no matching digit(s) satisfying "\d*" (f)
6 no matching digit(s) satisfying "\d*" (???)

 
Master Rancher
Posts: 43045
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaron, did you read the FAQ entry sridhar linked to?
 
Aaron Browne
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad; :roll:
Cheers
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic