Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

quantifier

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
import java.util.regex.*;

class Regex
{
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());
}
}
}

command line: java regex "\d*" ab34ef
output: 01234456

i got this from K&B 5 study guide(self test question).

i am not getting how the '*'(quantifier) and 'm.group()' works here?
please anyone explain me...


Preparing Scjp5
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That one caused me trouble too.

The trick is in what "0 or more" means and what it does.

string "AB34EF"
pattern "\d*"

Okay, '*' says search for "0" or more numeric digits. As it steps through the string, checking the digits it will return with a match for every check that returns 0 or more digits.

When you check 'A' there are 0 digits so matcher returns 0 for the index and the group() is null.

When you check 'B' there are 0 digits so matcher returns 1 for the index and the group() is null.

When you check '3' there is a digit but we need to keep searching so no return is made.

When you check '4' there is a digit and we need to continue searching...

When you check the 'E' that ends the sequence of digits so two things happen
For the digits we found, matcher returns 2 for the index and the group() is "34". Because the match used indexes 2 and 3, the next search will start at index 4 and it has 0 digits so matcher returns 4 and group() as null.

When you check 'F' 0 digits are found so matcher returns with 5 for the index and group() is null.

One more check is made to find the end of the string and it returns 6 as the index and I imagine null as the string.

As an intermediate step to illustrate how the answer is formed let's stack all of these together as follows...

0null1null2344null5null6null

Since null is actuall "no string at all" let's remove all of the nulls:

01234456

I may be a little fuzzy on what happens at the end of the string (the index 6) but I believe this reasonably well explains what happens on this question. I hope that it will be of some help.

Bob

(edited to correct typos)
[ October 06, 2008: Message edited by: Bob Ruth ]
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bob Ruth:
When you check 'A' there are 0 digits so matcher returns 0 for the index and the group() is null.


Well, not exactly. 'group()' returns the string that was matched by the previous 'find()'. In this case, it is the empty string ("") which is really different from 'null'.

Regards,
Thomas
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood '0123445' part. I did not get how there can be a 6 when the last index of the string passed is 5. Totally confused about how start() works when it reaches end of the string
 
Sandhya Bhaskara
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Probably m.find()it returns a true even after the end of string. Still confused..
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas, looking back I agree. Thanks for the correction. (after all I think if group returned null it would probably PRINT null!)
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob

i got some what cleared,but how that 6 comes at last?is it taking the next index also for matching?
 
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
This question is addressed in our SCJP FAQ.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bob

i got some what cleared,but how that 6 comes at last?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but how that 6 comes at last?


The FAQ entry marc linked to explains that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic