• 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

Question on Regex class in K&B

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends

This is a Practice Question from K&B for java 5. I have a doubt in it.

class Regex2 {

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

and it is executed as java Regex2 "\d*" ab34ef

then the output is 01234456

I dont get why we are getting 6 in the answer.

Thanks
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the regeular Expression is \d* it means or more digits

When comes to the string ab34ef the output is 01234456

Here 6 comes at the end because the expression is evaluated to true even after the 'f' which is at the end of string ab34ef
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A quick search through the archive uncovers discussion of this here, here, and here.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A new explanation has been added to the SCJP FAQ.

The key is that the asterisk (*) is a "greedy quantifier," specifying that whatever preceeds it should be matched zero or more times. By allowing for zero occurrances, a match of zero length is possible. And because a match of zero length is possible, the find() method will check the index following the last character of input.

For a match of zero length, the matcher's start() and end() methods both return the same index. And in these cases, the group() method returns an empty String (i.e., the substring from start() to end()).
 
If you look closely at this tiny ad, you will see five bicycles and a naked woman:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic