gpt4 book ai didi

python - 在数组中执行正则表达式搜索时,它返回为空

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

在数组中执行正则表达式搜索时,它返回为空

脚本

array = ['GW-date45:ger-date45:mySAPgives','DC-date48ccc:date48:mySAP']

# REGEX
hostname = []
for node in array:
hostname.append(re.findall(r'^[^-]*\K-([^:]+)', node))

for line in hostname:
print(line)

输出

[]
[]

REGEX101

最佳答案

Python re 不支持 \K construct .

您似乎甚至不需要它,因为您所需要的只是捕获组 1 的值。使用

import re
array = ['GW-date45:ger-date45:mySAPgives','DC-date48ccc:date48:mySAP']
hostname = []
for node in array:
m = re.search(r'^[^-]*-([^:]+)', node)
if m:
hostname.append(m.group(1))

for line in hostname:
print(line)

参见 Python demo .输出:

date45
date48ccc

关于python - 在数组中执行正则表达式搜索时,它返回为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58487146/

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