gpt4 book ai didi

python - 只匹配 IP 地址而不匹配其他号码

转载 作者:行者123 更新时间:2023-12-04 07:53:29 26 4
gpt4 key购买 nike

我希望以下正则表达式代码返回 IP 地址的输出,而不从源文件中将其他数字值作为 IP 返回。
代码:

import re

logdata = 146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
for item in re.finditer("(?P<host>[\d.]+)", logdata):
print(item.groupdict())
所需输出:
{'host': '146.204.224.152'}
不需要的输出:
{'host': '6811'}

最佳答案

我认为应该这样做:

(?P<host>(\d+\.){3}\d+)

关于python - 只匹配 IP 地址而不匹配其他号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66824660/

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