• 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

Regex Question with * quantifier from K&B

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am not clear on the Regex question (page 508 Question 1) from K&B 5 book

To uderstad I tried writing a similar program.

Lets say I give the followig commad line Java Regex "\d*" a
I get

0space
1space

I see that it is searchig for digits ad it finds 0 matches first time and prints postion 0 (search index 0). Then because we said * the start method returns to the previous index 0 now how come it goes to the postion 1 when there is only 1 charecter in the search strig.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
UseCodeTags when you post a code. Please Edit it and put code tags. Welcome to JavaRanch!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, What would happen you put + instead of *?
* - zero or more times
+ - one or more times.

Got it?
 
Raj Purushotham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abimaran,

Thank you for the respose. I tried with + istead of * and the results are same.

I can see that if I use "\d?" for the source a23a12 it gives me what I suspect

1space23
4space12

How ever if I use "\d*" for the source a23a12 it gives me the following:
0space
1space23
3space
4space12
6space

"6space" is not clear to me.


 
reply
    Bookmark Topic Watch Topic
  • New Topic