Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex Question with * quantifier from K&B

 
Raj Purushotham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic