正则表达式 \d* 的一个小问题

大罗罗的马拉松 2008-07-02 11:30:28
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“?
...全文
454 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 brz97 的回复:]
\d是永远不会去匹配字符的,永远只会匹配数字

楼主理解有误,,,最好还是听2楼的话,好好看看2楼推荐的文章

别的也就不多说了
[/Quote]

\d是永远不会去匹配字符的,永远只会匹配数字
但是\d*就会去匹配字符的, 我提问就是用\d*呀。
KOOK_OKKO 2008-07-04
  • 打赏
  • 举报
回复
第一次匹不到,第二次匹配到了,由于*是贪婪的,还想匹配第三次,所有就有第三次匹配行为
  • 打赏
  • 举报
回复
索引编号的理解,可以按照 String 中 substring 的用法来理解。

像 "a2".substring(0, 1) 就是截取索引 0 和索引 1 之间的字符串,也就是“a”。
  • 打赏
  • 举报
回复
如果你还是这样问的话,我在 2 楼让你去看的东西你可能就没去看过。

要理解这个问题,得先弄清楚 m.start() 和 m.end() 的输出表示什么意思?

实际上这两个输出的是字符的索引位,下面红色的部分是索引点编号,黑色的部分是
原来的文字:

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.

brz97 2008-07-04
  • 打赏
  • 举报
回复
\d是永远不会去匹配字符的,永远只会匹配数字

楼主理解有误,,,最好还是听2楼的话,好好看看2楼推荐的文章

别的也就不多说了
  • 打赏
  • 举报
回复
也谢谢楼上各位大侠的帮助。
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 bao110908 的回复:]
如果你还是这样问的话,我在 2 楼让你去看的东西你可能就没去看过。

要理解这个问题,得先弄清楚 m.start() 和 m.end() 的输出表示什么意思?

实际上这两个输出的是字符的索引位,下面红色的部分是索引点编号,黑色的部分是
原来的文字:

0a122

可以看出两个字符的话,那就存在三个索引点,即 0~2 号。

由于 * 的匹配方式是贪婪式的 0 到多个匹配。

先一步一步地来看匹配器的匹配过程:

1,匹配指…
[/Quote]

谢谢,这样的解析我就理解了。
gongyali2005 2008-07-03
  • 打赏
  • 举报
回复
\d*应该是配多个或零个数字!
KOOK_OKKO 2008-07-03
  • 打赏
  • 举报
回复
8楼12楼应该是对的,6楼是错的
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 varlj 的回复:]
Java code
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匹配…
[/Quote]


请问为什么会匹配第3次呢?匹配第1次a, 第2次匹配2,我都能理解,就是不知道为什么还来个第3次?明明"a2"字符长度只有2呀。
varlj 2008-07-03
  • 打赏
  • 举报
回复

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,这没什么好说的
第三次,和第一次一样的道理
rascalboy520 2008-07-03
  • 打赏
  • 举报
回复
http://blog.csdn.net/rascalboy520/archive/2008/06/03/2506444.aspx
看看这个吧,里面好多内容都是有用的,
havelock 2008-07-03
  • 打赏
  • 举报
回复
7楼的,奇了~~

Enter your regex: [0-9&&[3-5]]
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.

一模一样的啊~为什么答案不同捏
lord_is_layuping 2008-07-03
  • 打赏
  • 举报
回复
好像是我输了一个全角的4
havelock 2008-07-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bao110908 的回复:]
您的理解有误 \d* 绝对不可能去匹配字符 a 的

如果再加上个 m.end() 的话会发现一些有趣的东西

具体的可以看看 Java Tutorial 上关于正则表达式的部分,下面这个地址是译稿:
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

如果再有问题的话,可以进行回复
[/Quote]

穿马甲的~来把5楼号封了吧,太丑陋了.天天刷

回楼主:

\d匹配一个数字,但是你写的是 \d* ,其意义就是随便匹配不匹配了.
因为* == 0或者无限.

所以虽然a没有匹配\d,但由于*,还是会打印出来.

至于最后会打印2还是同样的原因.
lord_is_layuping 2008-07-03
  • 打赏
  • 举报
回复
请问一下火龙果,这个怎么回事啊?,是我哪里输错了吗
我用的是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:

KOOK_OKKO 2008-07-03
  • 打赏
  • 举报
回复
匹配"\0"啊
字符串后面还有个空白符\0的
zidasine 2008-07-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 bao110908 的回复:]
您的理解有误 \d* 绝对不可能去匹配字符 a 的

如果再加上个 m.end() 的话会发现一些有趣的东西

具体的可以看看 Java Tutorial 上关于正则表达式的部分,下面这个地址是译稿:
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

如果再有问题的话,可以进行回复
[/Quote]
正则高手的回复:)
dracularking 2008-07-03
  • 打赏
  • 举报
回复
d是digit
qiandongbo 2008-07-03
  • 打赏
  • 举报
回复
刚才说错了 可以仔细看下2个4前所空的位置,上面是一个字节,下面是一个字符,应该是输出4的格式还是什么导致的
加载更多回复(2)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧