aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regular Expression problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regular Expression problem" Watch "Regular Expression problem" New topic
Author

Regular Expression problem

Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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)?


~Sanjeev Singh<br />SCJP 1.5
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
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


SCJP 5
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
oops, it seems editor remove spaces
0 1 2 3 4 5 6
0a112b3245556 nothing at index 6!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41863
    
  63
The problem from K&B is covered in detail in the SCJP FAQ.


Ping & DNS - my free Android networking tools app
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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

Joined: Nov 01, 2006
Posts: 381
Thanks Ulf,
This hit right on the target.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression problem