• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex ???

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This FAQ should help.
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sridar,

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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read the FAQ entry Christophe pointed you to? It's rather detailed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic