Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Chap:6 selftest q: 1 but in argument little more  RSS feed

 
Rajni Patel
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is from chap:6 Self Test


import java.util.regex.*;

class Regex2
{
public static void main(String[] args)
{
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while (b = m.find())
{
System.out.print(m.start() + m.group());
}
}
}

And the command line : java Regex2 "\d*?" ab34ef so output is coming: 0 1 2 3 4 5 6
Please any one explain me how?
 
Devaka Cooray
Marshal
Posts: 5565
712
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is for SCWCD-related questions.
 
Rajni Patel
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But could you expain me how?

-Rajni
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've moved the question to the correct forum. This question is asked frequently so it is answered in the FAQ. Also please Use Code Tags when you post a source code...
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The FAQ contains the explanation for the regex expression - "\d*" however Rani is asking for the regex expression "\d*?".

Please explain the output. Even I am not able to understand that.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't see the question properly. Let's modify the program a bit to make it easier to understand


Now the output would be



As *? is reluctant, it will match as less as possible thus resulting in zero length matches. Does this make it clear??
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not in course for SCJP I guess
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am sorry but I am still confused . I have to know why index position 4 is not included in the output as it should have also been included in zero length match.

Please correct me if I am wrong.
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
indra negi wrote:I have to know why index position 4 is not included in the output as it should have also been included in zero length match.


In the output that I showed and in the output of the original question, there IS a zero length match at index 4. Can you please clarify further your question...
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I didn't see that properly.

Ok, why m.group() is not displaying 34 as the matched group as it does when we use "\d*" as the regex expression.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!