I was working through some of Mock questions provided by Kathy Sierra and Bert Bates and had a question about regular expressions. Maybe someone here can clear things up for me.
It's question #7 from the mock questions that they posted to the list.
I'm modified the code slightly so it's clearer as to what output is coming from what method. My version of their code is as follows:
The output is:
0: 1: 2: 4: 5: 6:
As I understand what's happening, the find() method is walking the String ab34ef from left to right, looking for matches. If it finds one, then it's available via the group() method. If it doesn't find one, then you get a zero-length String from the group method. For position 0 it finds "a" (which doesn't match so we get "0:". For position 1 it finds "b" (which doesn't match) so we get "1:". For position 2 it finds "34" (a match) and so we get "2:". Position 3 is gobbled up by the match, so we don't get output for it. For position 4 it finds "e" (which doesn't match) so we get "4:". For position 5 it finds "f" (which doesn't match) so we get "5:". For position 6 it finds???
That's my question. I'm not sure why we have a 6th find. Is there some sort of implied String terminator ($ in Perl regex speak) that it's finding?
I think that's exactly right, Barry. The Java tutorial on the topic says:
A zero-length match can occur in a several cases: in an empty input string, at the beginning of an input string, after the last character of an input string, or in between any two characters of an input string.
Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Joined: Aug 22, 2005
Clever Barry. :-)
And thanks for the confirmation Ryan and Bert.
A co-worker and I puzzled over that one a bit and were leaning towards a zero-length string, a null string, some sort of invisible terminator etc. It's just nice to see in writing what is actually happening. Helps to "convince me" too. :-)