![为什么我在java中一次Matcher.find()的执行中得到所有的匹配?[重复] 为什么我在java中一次Matcher.find()的执行中得到所有的匹配?[重复]](/img/no_img.png)
我的输入字符串是
"PING www.abc.com (172.217.160.132) 56(84)字节的数据。
64字节来自maa03s29-in-f4.1e100.net(172.217.160.132):icmp_seq=1 ttl=51 time=50.9 ms。
来自 maa03s29-in-f4.1e100.net (172.217.160.132) 的 64 个字节:icmp_seq=2 ttl=51 time=67.0 ms。
来自 maa03s29-in-f4.1e100.net (172.217.160.132) 的 64 个字节:icmp_seq=3 ttl=51 time=81.6 ms。
而我的模式是
(\\d+ bytes from [\\w\\W]+ \\(\\d+(\\.\\d+){3,3}\\): icmp_seq=\\d+ ttl=\\d+ time=[\\d+.]+ ms)
我执行
int count++;
while(matcher.find()) {
count++;
System.out.print(count+" ");}
我的预期产出 1 2 3 但我的实际产量1当我试图在 while 循环中打印 matcher.group 时,我得到的是
来自 maa03s29-in-f4.1e100.net (172.217.160.132) 的 64 个字节:icmp_seq=1 ttl=51 time=50.9 ms。
来自 maa03s29-in-f4.1e100.net (172.217.160.132) 的 64 个字节:icmp_seq=2 ttl=51 time=67.0 ms。
来自 maa03s29-in-f4.1e100.net (172.217.160.132) 的 64 个字节:icmp_seq=3 ttl=51 time=81.6 ms。
为什么在第一次执行时就能找到所有的匹配?
投票
[...] [\\w\\W]+ [...] .+[...]
(\\d+ bytes from .+ \\(\\d+(\\.\\d+){3,3}\\): icmp_seq=\\d+ ttl=\\d+ time=[\\d+.]+ ms)
# ^^
[\\w\\W]+ [...]
from [...]
(172.....)
.+ [...]
[...]