• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Matcher.find() APIs unexpected Behaviour

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

I am not able to understand un-expected behaviour of find() API of Matcher class.
As per the java documents and my understanding, find() method return TRUE if system is able to find more occurence of sequence that matches given the pattern.

public class RejEx {

public static void main(String[] args) {
String source = "pp";
Pattern p = Pattern.compile("p*");
Matcher m = p.matcher(source);
while(m.find())
{
System.out.println("Pattern Found :"+m.group());
}
}
}

As per the code snippet,anybody will expect that while loop should get executed only once but it gets executed 2 times. So when I run this program I get output as below :
Pattern Found : pp
Pattern Found :

I does work fine if I use "p+" pattern but if use "p*" or "p?" as pattern than I am not getting output as per my expectation.

So now I have following questions :
1. Is my understanding not correct ?
2. Is there any BUG in find() API ?

cheers,
Sunil

[ May 14, 2008: Message edited by: Sunil Dixit ]

[ May 14, 2008: Message edited by: Sunil Dixit ]
[ May 14, 2008: Message edited by: Sunil Dixit ]
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The javadocs of Matcher.group actually mention why this is happening. (Well, they do for "a*", but the principle still holds :-)

After reading that you may wonder why the same isn't happening at the beginning of the string - for an answer to that, read up on the concept of "greedy matching" (which is what the "*" special character does).
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot buddy !
 
reply
    Bookmark Topic Watch Topic
  • New Topic