• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on Pattern and Matcher!

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All

Can anybody help out with the below example
i dont understand it.




and the command line invocation is
java Regex2 "\d*" ab34ef


A). 234
B). 334
C). 2334
D). 0123456
E). 01234456
F). 12334567
G). Compilation fails

the correct answer is E.Need explnation on this.

As per i have studied from K&B
it says \d is for finding digits within
the source code ,the pattern given is "\d*"
means find Zero or more digits.
so i think the answer should be
23.........bcoz only at 23 position digits exist.

but how the answer is as above i dont understand
where i am going wrong.
please if anyone can clear my doubt it will
be realy kinda of you

thanks in advance.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
\d = digit
* = zero or more (0->)

Pattern: \d* (Read: Zero or more digits, it means that every character, alphabets are included)

So, the sequence is: ab34ef

a = zero (.start() = 0, .group() = '' ) printing: 0
b = zero (.start() = 1, .group() = '' ) printing: 1
34 = one (.start() = 2, .group() = 34 ) printing: 234
e = zero (.start() = 4, .group() = '' ) printing: 4
f = zero (.start() = 5, .group() = '' ) printing: 5
= zero (.start() = 6, .group() = '' ) printing: 6

We're into six since indexes: 0-1 = a, 1-2 = b, 2-4=34, 4-5=e, 5-6=f

Every character is counted but, IF it's NOT digit, group() does not print anything. (start() still prints)

Note, that 34 is counted as ONE occurrence.

Hope this helps something...
[ December 06, 2007: Message edited by: Jari Timonen ]
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dhwani,

This question has caused so much confusion that it has its own entry in the SCJP FAQ.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thr


Thanks a lot Jari Timonen
for such a gud explanation i am
now clear with my doubts.
Thanks to Kelvin Lim for
showing the correct link towards
my problem.





Dhwani->Its always too soon to quit.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic