• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression problem

 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In course of solving the promlem#1 page 498 K&B I encountered the following confusion?
I modified the program to understand it better and finds the following result.
--------------------------------------------------------------
Enter your regex:
\d*
Enter input string to search:
ab34ef

I found the text "" starting at index 0 and ending at index 0.
I found the text "" starting at index 1 and ending at index 1.
I found the text "34" starting at index 2 and ending at index 4.
I found the text "" starting at index 4 and ending at index 4.
I found the text "" starting at index 5 and ending at index 5. <<<<< Mark 1
I found the text "" starting at index 6 and ending at index 6.
--------------------------------------------------------------

Till <<<<< Mark it is clear to me the last line is puzzling me.Where is the index 6 came from in a string having length 6 (ab34ef)?
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I did not read your question because I have time up signal, and I have to get back to my work, but I spot "*" quantifier which causes problems to every one in the first usage of it, this qunatifier means "0 or more occurances", think about it, if you have no occurances this is it self is an occurance, for example

pattern "\d*"
means search for zero or more digits, so regex engine will do


output is

0
1 1
2
3 255
6

when you trace this program assume indexes
0 1 2 3 4 5 6
a 1 b 2 5 5 nothing at index 6!!

I hope that is helpful
 
Costa lamona
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, it seems editor remove spaces
0 1 2 3 4 5 6
0a112b3245556 nothing at index 6!!
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem from K&B is covered in detail in the SCJP FAQ.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mohammed,
During the course of time when I didn't receive any answer I tried myself and concluded the things you spoted.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf,
This hit right on the target.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic