This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Doubt in Parsing(Regex) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt in Parsing(Regex)" Watch "Doubt in Parsing(Regex)" New topic
Author

Doubt in Parsing(Regex)

Dash Abhisek
Ranch Hand

Joined: Jul 20, 2008
Posts: 63
pattern("aaaaa");--->5 a's
matcher("a*");
while(find())
system.out.println(start()) produces....

0
1
2
3
4
5---->///now, iam understanding why it's printing uptill 4 but why its printing "5"?
please explain...
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Originally posted by Dash Abhisek:
sorry the pattern for the question that i posted earlier is "a?"

please explain


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dash Abhisek
Ranch Hand

Joined: Jul 20, 2008
Posts: 63
Thanks for your reply but if you could explain it right here then it would be really helpful
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I'd be happy to lend a hand (as I am sure others would be), but could you please post your complete code snippet rather than pseudo code. As posted right now, it looks like the code is using some method pattern() that takes a string. There is no such method in the Java API anywhere. So I have no idea what the code is doing there. And I can only assume the find() method and start() methods in the code are the find() method from the Matcher class... but that's a big assumption.

Please see the FAQ tip PostRealCode for more info.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

After the "aaaaa" string there is one substring left: the empty string. This matches a* (zero or more a's) too.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
This thread is much more confusing than it needs to be. Dash's pseudocode implies that he's matching the regex "aaaaa" against the target string "a*", which is backward. Then we learn from the the quoted text in Marilyn's reply (quoted from where, I wonder? - edit: never mind, it came from here) that the regex is really "a?", which is good, because "a*" is inconsistent with the output. Dash, this is why you should post your real code whenever possible, as Mark suggested.

Anyway, Rob's answer is still essentially correct despite the changed regex. Here's a complete demonstration: output: As you can see, each match starts where the previous one ended. Since the regex can match zero characters, it matches the empty string starting at index 5.
[ July 20, 2008: Message edited by: Alan Moore ]
Dash Abhisek
Ranch Hand

Joined: Jul 20, 2008
Posts: 63
Thanks Alan for demonstrating the answer but In the String I have given five a so the first a is at position 0 and the fifth a is position at 4th position then how come 5th index is there ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Please read the replies, particularly Rob Prime's which explains it simply.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Dash Abhisek:
Thanks Alan for demonstrating the answer but In the String I have given five a so the first a is at position 0 and the fifth a is position at 4th position then how come 5th index is there ?


The regex "a?" in combination with the string "aaaaa" will match on the following places:

Dash Abhisek
Ranch Hand

Joined: Jul 20, 2008
Posts: 63
Thanks to Everybody who gave Reply

My doubt is cleared
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Parsing(Regex)
 
Similar Threads
Doubt on Pattern and Matcher!
Doubt in formatting
String replace method
Doubt About Strings
A simple Array Problem