Matcher: groupCount()
int groupCount()
- Returns the number of capturing groups in this matcher's pattern.
/*
Group 0: 1
Group 1: 1
Group 2: null
Group 3: null
Group 0: 2
Group 1: 2
Group 2: null
Group 3: null
Group 0: 3
Group 1: 3
Group 2: null
Group 3: null
Group 0: 4.5
Group 1: 4.5
Group 2: .5
Group 3: null
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainClass {
public static void main(String[] av) {
String regEx = "[+|-]?(\\d+(\\.\\d*)?)|(\\.\\d+)";
String str = "a b c d e 1 2 3 4.5 ";
Pattern pattern = Pattern.compile(regEx);
Matcher m = pattern.matcher(str);
while(m.find()) {
for(int i = 0; i<=m.groupCount() ; i++) {
System.out.println("Group " + i + ": " + m.group(i)); // Group i substring
}
}
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
Matcher:
- Regular Expression Processing
- Normal character
- Wildcard character
- Using Wildcards and Quantifiers
- Greedy behavior
- Working with Classes of Characters
- Using replaceAll( )
- Using split( )
- Matcher: appendReplacement(StringBuffer sb,String replacement)
- Matcher.appendTail(StringBuffer sb)
- Matcher: find()
- Matcher: group()
- Matcher: group(int group)
- Matcher: groupCount()
- Matcher: lookingAt()
- Matcher: matches()
- Matcher: replaceAll(String text)
- Matcher: start()