wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes groupcount() method in MatchResult Interface in Java 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "groupcount() method in MatchResult Interface in Java 5" Watch "groupcount() method in MatchResult Interface in Java 5" New topic
Author

groupcount() method in MatchResult Interface in Java 5

Sarada Bikkina
Greenhorn

Joined: Jan 30, 2006
Posts: 9
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


Thanks N Regards,<br /> Sarada Bikkina
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: groupcount() method in MatchResult Interface in Java 5
 
Similar Threads
Scanner
Mock question java.util.regex.Pattern - drag and drop
Doubt regarding regex
Scanner question or maybe it may be more about java class API's
scanner question