在数组中执行正则表达式搜索时,它返回为空
脚本
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)
输出
[]
[]
最佳答案
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/