Forums Register Login

Possibly a bug in kathy Sierra

+Pie Number of slices to send: Send
I was going through chapter 6 of kathy sierra. In Objective 3.5, subtopic "Searches Using Quantifiers", it is creating a list of file whose name starts with proj1. the exp it gives is: proj1([^,])* .
I think the expression is wrong as this expression searches for filename containing proj1 not starting with proj1. The pattern should be
,proj1([^,])*
+Pie Number of slices to send: Send
Hello "Ankit Newbee "-

welcome to JavaRanch.

I think it's important to note that any bug would be in Kathy's book, not in Kathy herself

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.
[ December 03, 2007: Message edited by: Ulf Dittmer ]
+Pie Number of slices to send: Send
Hi Ankit,
What Kathy has given is correct as far as i think. The expression proj1([^,])* go through the series and will fetch all combinations starting with proj1 (as * is there at end) but as [^,] is there so it wont go further and will stop when it meets a ,, then it will search another pattern like the same.What you have given ,proj1([^,])* will look for all starting with , and this will give you only one pattern.. the very 1st one, that too starting with , and no others as you have given [^,] it will leap over , and find the next one but wont find any with , again. Hope this clears now....
+Pie Number of slices to send: Send
Hi Dittmer, thanks for your suggestion.

Pranav, I agree with your point that ,proj1([^,])* can't be the solution. But my point is expression proj1([^,])* doesn't specify that filename starts with proj1. For example a filename testproj1 will also be correct based on this expression.
+Pie Number of slices to send: Send
 

But my point is expression proj1([^,])* doesn't specify that filename starts with proj1. For example a filename testproj1 will also be correct based on this expression.



Ankit,
What you are saying about "testproj1" would have been the result when the pattern you are searching would be *proj1([^,]) as this will search for all words that ends with proj1 in their name like they all could be proj1,testproj1,test1proj1,myproj1... as * means zero or more occurances. Whereas if you put * at the end it will give you all patterns that starts with proj1 and whatever is there after that.. here all searches would feed you with proj1,proj1star,proj1proj2.... Hope you got it..
+Pie Number of slices to send: Send
Even if you won't put * in the beginning, it will search for that because you are not specifying that p is the first character of the filename. You can try it in eclipse. I don't know how you can specify that filename starts with proj1.
+Pie Number of slices to send: Send
This is beyond the scope of the SCJP, but you can use the regex (^|,)proj1([^,])* to ensure that the filenames start with "proj1". Note that the caret in (^|,) is not the negation operator in [^,]; rather, this caret denotes "the start of the string". You can correspondingly use $ to denote "the end of the string".

(Of course, you'll now need to clean up the possible leading comma in the returned matches. The best way to avoid this problem is to use a regex capturing group, but that's again outside the scope of the SCJP.)
+Pie Number of slices to send: Send
Thanks Kelvin.

I was just trying (^|,)proj1([^,])* expression on eclipse and i think for start (|,) is the correct expression because its giving me correct answer .

But for now its enough that these things are out of scope of scjp.
+Pie Number of slices to send: Send
I am sorry. I got confused in brackets. both [(^|,)] and [|,] are giving me the correct result.
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 706 times.
Similar Threads
Question about regex
HFSJ
Regex; Unable to follow difference in output
String Immutability
Question About Regex Chapter-6 K&B
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:49:34.