gpt4 book ai didi

python - Python 中的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 12:29:20 24 4
gpt4 key购买 nike

我是 python 的新手,想在 Python 中运行代码。
我写的代码是

#!/usr/bin/python

import sys
import re

for line in sys.stdin:
results = re.search('ipAddress=([^&]*])', line)
if len(results.group(1)) != 0:
print results.group(1)

需求是在一个字符串中找到IP地址。字符串将是这样的

blah....ipAddress=173.110.208.118&deviceId

Output: 173.110.208.118

请让我知道我做错了什么..

最佳答案

我认为你的 ] 太多了。尝试

results = re.search('ipAddress=([^&]*)', line)

关于python - Python 中的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5681642/

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