我对以下例子有异议:
import java.util.regex.*;
class Regex2 {
public static void main(String[] args) {
Pattern p = Pattern.compile(args[0]);
Matcher m = p.matcher(args[1]);
boolean b = false;
while(b = m.find()) {
System.out.print(m.start() + m.group());
}
}
}命令行:
java Regex2 "\d*" ab34ef有人能解释一下吗,为什么结果是: 01234456
regex模式是d* -意思是1号或多个,但在args1中有更多的位置,
谢谢
发布于 2013-08-22 15:35:37
\d*匹配0或更多位数。因此,它甚至会在每个字符之前和最后一个字符之后匹配空字符串。首先在索引0之前,然后在索引1之前,等等。
因此,对于字符串ab34ef,它匹配以下组:
Index Group
0 "" (Before a)
1 "" (Before b)
2 34 (Matches more than 0 digits this time)
4 "" (Before `e` at index 4)
5 "" (Before f)
6 "" (At the end, after f)如果您使用\\d+,那么您将在34只得到一个组。
https://stackoverflow.com/questions/18384930
复制相似问题