I'm writing a very simple sample code about regular expression but failed to work with group
.
The regular expression is: rowspan=([\\d]+)
The input string is: <td rowspan=66>x.x.x</td>
I'm testing it on online regex engine and obvious the group 66
can be captured, see snapshot below:
Based on the javadoc,
Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().
So I think there should be two groups and group 0 should be rowspan=66
, the group 1 should be 66
. However, all I can get from below code is the former.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String args[]){
String input = "<td rowspan=66>x.x.x</td> ";
String regex = "rowspan=([\\d]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if(matcher.find()){
for(int i = 0; i < matcher.groupCount(); i++){
System.out.println(matcher.group(i));
}
}
}
}
The output is:
rowspan=66
Thanks for your help in advance.