Java 交替(|)和+;一语道破
是否可以解释java正则表达式的以下输出:(案例1)Java 交替(|)和+;一语道破,java,regex,Java,Regex,是否可以解释java正则表达式的以下输出:(案例1) String s=“topcoder是” 字符串p=“(顶部编码器)+”; Pattern-pttrn=Pattern.compile(p); 匹配器m=pttrn.匹配器; while(m.find()){ System.out.println(“组计数:+m.groupCount()); 对于(int i=1;i当使用重复的捕获组时,即由于一个量词,Matcher将只捕获最后一个匹配 您可以将整个重复捕获组包装在其自己的捕获组中,以提取每
String s=“topcoder是”
字符串p=“(顶部编码器)+”;
Pattern-pttrn=Pattern.compile(p);
匹配器m=pttrn.匹配器;
while(m.find()){
System.out.println(“组计数:+m.groupCount());
对于(int i=1;i当使用重复的捕获组时,即由于一个量词,Matcher
将只捕获最后一个匹配
您可以将整个重复捕获组包装在其自己的捕获组中,以提取每个匹配项
String p = "((top|coder)+)";
如中所述。当您使用量词重复捕获组时,上一个捕获将被下一个捕获覆盖。
String s = "topcoder is ";
String p = "(top|coder)";
Pattern pttrn = Pattern.compile(p);
Matcher m = pttrn.matcher(s);
while(m.find()){
System.out.println("group count: " + m.groupCount());
for (int i = 1; i <= m.groupCount(); i++) {
System.out.println("Found : \"" + m.group(i) + "\" as group " + i);
}
System.out.println("Zeroth group:" + m.group(0));
}
String p = "((top|coder)+)";