• 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

Matcher problem....

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

I have this program

Pattern p = Pattern.compile("<id>(.*?)</id><fn>(.*?)</fn><fl>(.*?)</fl>");
String candidateString = "<file><id>4</id><fn>abcd</fn><fl>c:</fl></file>";
Matcher matcher = p.matcher(candidateString);

int numberOfGroups = matcher.groupCount();
System.out.println("numberOfGroups =" + numberOfGroups);

I am expecting numberOfGroups should be 1 but it gave me 3 why? and how will get count 1 ? any help?



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

matcher.groupCount() returns the number of capturing groups in this matcher's pattern. In your pattern you have used (.*?) 3 times.

You can get count by matching the entire string



Regards,
Antany.
reply
    Bookmark Topic Watch Topic
  • New Topic