Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a2");
boolean b = false;
while(m.find()) {
System.out.println(m.start() + m.group());
}输出结果:
0
12
2是不是这样理解: \d* 匹配 a, 打印0, \d* 匹配 2, 打印12, 最后那个2是如何来的,是匹配”\n“?
Matcher m = p.matcher("a2");
boolean b = false;
while(m.find()) {
System.out.println(m.start() + m.group());
}输出结果:
0
12
2是不是这样理解: \d* 匹配 a, 打印0, \d* 匹配 2, 打印12, 最后那个2是如何来的,是匹配”\n“?
楼主【lxqluo】截止到2008-07-02 23:30:46的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
http://www.java2000.net/doc/Java.Regex.Tutorial/Java.Regex.Tutorial.html你的这个问题可以看看
http://www.java2000.net/doc/Java.Regex.Tutorial/Java.Regex.Tutorial.html#reg5_1如果再有问题的话,可以进行回复
我用的是RegexTestHarnessV5.java
Console内容拷贝如下:
Enter your regex: foo
Enter input string to search: foo
I found the text "foo" starting at index 0 and ending at index 3.Enter your regex: foo
Enter input string to search: u are a big fool;
I found the text "foo" starting at index 12 and ending at index 15.Enter your regex: foo
Enter input string to search: foo id foo;
I found the text "foo" starting at index 0 and ending at index 3.
I found the text "foo" starting at index 7 and ending at index 10.Enter your regex: cat.
Enter input string to search: cats
I found the text "cats" starting at index 0 and ending at index 4.Enter your regex: cat.
Enter input string to search: cat
No match found.Enter your regex: \Qcat.\E
Enter input string to search: cats
No match found.Enter your regex: cat\Q.\E
Enter input string to search: cats
No match found.Enter your regex: [0-4[6-8]]
Enter input string to search: 5
No match found.Enter your regex: [0-4[6-8]]
Enter input string to search: 1
I found the text "1" starting at index 0 and ending at index 1.Enter your regex: 51
Enter input string to search:
No match found.Enter your regex: [0-4[6-8]]
Enter input string to search: 51
I found the text "1" starting at index 1 and ending at index 2.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 3
I found the text "3" starting at index 0 and ending at index 1.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 33
No match found.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 4
No match found.Enter your regex: [0-9&&[345]]
Enter input string to search: 333
I found the text "3" starting at index 0 and ending at index 1.
I found the text "3" starting at index 1 and ending at index 2.
I found the text "3" starting at index 2 and ending at index 3.Enter your regex: [0-4[6-8]]
Enter input string to search: 7
I found the text "7" starting at index 0 and ending at index 1.Enter your regex: [0-4[6-8]]
Enter input string to search: 777
I found the text "7" starting at index 0 and ending at index 1.
I found the text "7" starting at index 1 and ending at index 2.
I found the text "7" starting at index 2 and ending at index 3.Enter your regex: [0-4[678]]
Enter input string to search: 777
I found the text "7" starting at index 0 and ending at index 1.
I found the text "7" starting at index 1 and ending at index 2.
I found the text "7" starting at index 2 and ending at index 3.Enter your regex: [0-4[678]]
Enter input string to search: 666777888
I found the text "6" starting at index 0 and ending at index 1.
I found the text "6" starting at index 1 and ending at index 2.
I found the text "6" starting at index 2 and ending at index 3.
I found the text "7" starting at index 3 and ending at index 4.
I found the text "7" starting at index 4 and ending at index 5.
I found the text "7" starting at index 5 and ending at index 6.
I found the text "8" starting at index 6 and ending at index 7.
I found the text "8" starting at index 7 and ending at index 8.
I found the text "8" starting at index 8 and ending at index 9.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 444
I found the text "4" starting at index 0 and ending at index 1.
I found the text "4" starting at index 1 and ending at index 2.
I found the text "4" starting at index 2 and ending at index 3.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 333
I found the text "3" starting at index 0 and ending at index 1.
I found the text "3" starting at index 1 and ending at index 2.
I found the text "3" starting at index 2 and ending at index 3.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 444
I found the text "4" starting at index 0 and ending at index 1.
I found the text "4" starting at index 1 and ending at index 2.
I found the text "4" starting at index 2 and ending at index 3.Enter your regex: [0-9&&[3-5]]
Enter input string to search: 4
I found the text "4" starting at index 1 and ending at index 2.Enter your regex:
穿马甲的~来把5楼号封了吧,太丑陋了.天天刷回楼主:\d匹配一个数字,但是你写的是 \d* ,其意义就是随便匹配不匹配了.
因为* == 0或者无限.所以虽然a没有匹配\d,但由于*,还是会打印出来.至于最后会打印2还是同样的原因.
Enter input string to search: 4
No match found.
Enter your regex: [0-9&&[3-5]]
Enter input string to search: 4
I found the text "4" starting at index 1 and ending at index 2. 一模一样的啊~为什么答案不同捏
看看这个吧,里面好多内容都是有用的,
Pattern p = Pattern.compile("\\d*");
Matcher m = p.matcher("a2");
boolean b = false;
while(m.find()) {
System.out.println(m.start() +":"+ m.group());
} 如果你改成这样,大概就能明白为什么会那样了,结果是
0:
1:2
2:解释一下:
\d本身是匹配数字的,而*是匹配0次或多交(注意,0次也算成功)
然后分析一下你的程序,当第一次匹配的时候,就是在位置0的时候,\d匹配了0次,但是没有任何数据,所以输出了
0:
然后第二次匹配,匹配到了2,这没什么好说的
第三次,和第一次一样的道理
原来的文字:0a122可以看出两个字符的话,那就存在三个索引点,即 0~2 号。由于 * 的匹配方式是贪婪式的 0 到多个匹配。先一步一步地来看匹配器的匹配过程:1,匹配指示器开始位于字符“a”处,由于 a 不能匹配 \d,因此,这时产生了零宽度匹配,
就只匹配到了索引为 0 的零宽度字符,这时的 m.start 输出的是 0,如果你也使用了 m.end
的话,输出的也是 0。start 和 end 相同时,就是产生了零宽度匹配,也就是匹配了空串。2,匹配指示器继续下移到字符“2”处,这时“2”能匹配 \d,再由于 * 是贪婪的,因此零宽
度匹配在这里是不成功的,最大地匹配到了字符 2,这时 m.start 输出的是 1,而 m.end 输出
的是 2。start 和 end 正好位于 2 的左右两边,所以是匹配了字符 2。3,匹配指示器再下移,但是后面没有字符了,只剩下个索引点 3,因此索引点 3 也满了 * 的
0 个匹配,这时也产生了零长度匹配,m.start 输出的是 3,m.end 输出的也是 3。所匹配到的
字符串是也是个空串。根据 Java Tutorial 中提供的正则表达式测试工具,测试结果如下(我在 2 楼的回复中的第一个链接中
有分别适用于 JDK 1.4、JDK 1.5、JDK 1.6 的源代码):Enter your regex: \d*
Enter input string to search: a2
I found the text "" starting at index 0 and ending at index 0.
I found the text "2" starting at index 1 and ending at index 2.
I found the text "" starting at index 2 and ending at index 2.
\d是永远不会去匹配字符的,永远只会匹配数字
但是\d*就会去匹配字符的, 我提问就是用\d*呀。