This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex ???

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


A 234
b 334
c 2334
d 0123456
e 01234456
f 12334567
g compilation fails

This is a Question from K&B chapter 6
The answer I got after compiling was 0.
The answer mentioned is "e is correct " even though later it says a,b,c,d,e,f,g are incorrect due to the above reason.

I believe there is a typo somewhere, but either way I cannot understand the result or the question.And i cannot understan the options given to chose the answer.They are no where even close to what i thought would be the result.

Not sure what exactly does "\d*" means.
I think it is Zero or more digits, But can somebody specify how the matching take place and also the use of group().

Thanks.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This FAQ should help.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
if (b=m.find())
System.out.println(m.start() + m.group());
}
}


Hi Nabila, If you change the if(b=m.find()) to while(b=m.find()) it should work and print 01234456.

Thanks Christopher i had the same doubt about the 6th index position

[ April 08, 2008: Message edited by: sridhar row ]
[ April 08, 2008: Message edited by: sridhar row ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sridar,

Can you please expalin me the answer, how it came?

 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the FAQ entry Christophe pointed you to? It's rather detailed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic