• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Regex doubt

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am having hardtime understanding this question. Can someone pl clarify.

This is from Self Test K&B Ch-6.


The answer is 01234456

Thanks
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Pav:

You are passing "\d*" as pattern, remember that the '*' modifier means ZERO OR MORE, so your program is asking for any place in which it found ZERO OR MORE DIGITS, that�s why the output show you all positions. The out put means:
"there are 0 digits in position 0,
there are 0 digits in position 1,
the digits '34' are in position 2,
there are 0 digits in position 4,
there are 0 digits in position 5,
there are 0 digits in position 6"
Remember that this Regex methods return the initial position and the matches.

Regards,
 
Alejandro Galv�n
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to use the ONE OR MORE modifier ('+'), and you will see the out put: 234, which means in position 2 are the digits '34'
 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent Reply Alejandro!

I had troubles myself understanding how that Regex Syntax worked. Now I've got it.! and that's so Cool.

Good Luck,
Jose
 
author
Posts: 23926
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is ask so often here, that a search on "regex2" would have save time a lot of time. This question is also in the Javaranch FAQ :

http://faq.javaranch.com/java/ScjpFaq#kb-regexp

Henry
 
Pav Arora
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome answer!! Thanks Alejandro.

But why is it printing 6 in the end. The string length is 6 ie if started from 0 will go till 5 ??

Thanks
-p
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hope, the following link will clear your doubts.

http://faq.javaranch.com/java/ScjpFaq#kb-regexp

It will be a nice idea to change the metacharacter, and try with "\\d+","\\d?"
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pav:

Look at the link above given by Dean. I just checked right now, I think is very well explained. If you still having doubts, post them.

Regards,
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic