• Post Reply Bookmark Topic Watch Topic
  • New Topic

Matcher problem....  RSS feed

 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!