Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

groupcount() method in MatchResult Interface in Java 5

 
Sarada Bikkina
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frnds,
I was actually going through a program using scanner where i found the MatchResult Interface.Here is the block of code which i went through

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input);
s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
MatchResult result = s.match();
for (int i=1; i<=result.groupCount(); i++)
System.out.println(result.group(i);
s.close();

and the ouptut is :
1
2
red
blue

i didnt understand the funtionality of the groupCount() and the group() methods.can anyone please explain me?

Thanks in Advance
 
Henry Wong
author
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didnt understand the funtionality of the groupCount() and the group() methods.can anyone please explain me?


Unfortunately, there is no simple explanation -- if you don't know what Regular Expressions are. The match() method returns the last Regex match object used. The group and group count are related to Regex groups. In this example, the groups are the parts of the match between the "(" and ")", of the regex pattern used by the findInLine() method.

For more information, you will need to google some tutorials on Regex.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic