• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Confused About Greedy Quantifiers..please help!!!

 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
I am confused about greedy quantifiers a lot..... I am getting the concepts which are given in the book..... but when i am trying to solve some questions on teh same I am forced to believe that i am not at all clear with the concepts....

Here is what I am confused about:

In page no.481

The greedy quantifier does in fact read teh entire source data, and tehn it works backwards(from the right) until it finds the rightmost match.At that point, it includes everything from earlier in the source data upto and including teh data that is part of the rightmost match.


this para is clear to me(at least i think so)

But when i am applying this to the following question,I am



Compiling and runnning the code by giving the following command

java Regex2 \d* ab34ef

I expected teh output to be 234

But teh output is 0123445..........why is this???
I think I am somewhere lacking in understanding the greedy quantifiers....please exaplin me in details and of course you can provide any useful links which can make my concepts good enough to tackle exam questions
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dabasmitha
In the expression you are specifying
\d* it means that it searches for digits 0 or more occurances ie
the pattern is something like ab34ef.
a-->0 //0 occurance of digit
b-->1 //0 occurance of digit
34-->2 //since we have given \d* the starting index of digit is 2 and the digit occurance is 34. so the matches till now is 01234 ok.
e-->4 //0 occurance of digit
f-->5 //0 occurance of digit
These matches will also be added to the mathcing pattern which we are searching. so now the full match will be 0123445 right. If you are clear thats good and enough.

Regards
Manju
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manju,
The explaination you have provided is understood... But I want to know whethr this \d* is a greedy quantifier or not...
and if it is, then according to the concept given in Pg 481
it should print only 234 as it satrts searching from the right of the expression and when the match is found it prints the matching index(which start method returns) and the matching group(which the group method returns).
why should it go on printing each and every index???
please calrify...........
[ July 15, 2007: Message edited by: debasmita pattnayak ]
 
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
See our SCJP FAQ: Could someone explain the Regex2 class from K&B's Java 5 Study Guide?.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by debasmita pattnayak:
But teh output is 0123445..........why is this???


Small correction. The result is 01234456
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The greedy quantifier does in fact read teh entire source data, and tehn it works backwards(from the right) until it finds the rightmost match.At that point, it includes everything from earlier in the source data upto and including teh data that is p
 
Manjunath Hirennavar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The greedy quantifier does in fact read teh entire source data, and tehn it works backwards(from the right) until it finds the rightmost match.At that point, it includes everything from earlier in the source data upto and including teh data that is part of the rightmost match.

As upto my understanding the explaination goes like below

Read it carefully
It includes all indexes but when it matches to a digit here it is 34 watch clearly that the index of 4 is not included and the output is 0123445 if your pattern were to be ab2749ef then the output would be 012274967 clearly the indexes of 749 are not included because the 2749 is considered as one occurance as a result of * and hence only starting index is included. But other than digits every character is treated as single and 0 occurance.
This is what i think is the meaning of above paragraph.

Manju.
 
debasmita pattnayak
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
Now its clear for me......

thanks marc for providing that link....

 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by debasmita pattnayak:
as it starts searching from the right of the expression and when the match is found it prints ...

This is only important when you have two possible matches, and the first match is included within the second match. In this case the longest match is returned. For example:

.*foo xfooxxxxxxfoo

There are two potential matches:
xfoo and
xfooxxxxxxfoo

Since the greedy quantifier searches from right to left for the end of the expression the result is xfooxxxxxxfoo.
 
What are you doing in my house? Get 'em tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic