gpt4 book ai didi

python - 正则表达式: Get digit from the ping command output

转载 作者:行者123 更新时间:2023-11-30 22:48:17 25 4
gpt4 key购买 nike

a = r'5 packet(s) received'
reg = re.compile(r'(\d*) packet(s) received')
result = re.findall(reg,a)
for i in result:
print i

我是Python的初学者。我必须编写一个脚本来检查链接状态。在“Ping”命令的输出中,我必须捕获字符串中收到的包的数量,例如。 收到 5 个数据包。我对 re 的尝试不起作用。有人可以纠正我的错误吗?

最佳答案

括号在正则表达式中具有特殊含义。他们创建团体。为了真正匹配括号,它们应该用 \ 进行转义。

所以,替换它:

reg = re.compile(r'(\d*) packet(s) received')

这样:

reg = re.compile(r'(\d*) packet\(s\) received')

关于python - 正则表达式: Get digit from the ping command output,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40232501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com