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

标签 python regex

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

脚本

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/

相关文章:

python - 将多个列字符串连接成一列

python - 引用 ndarray 中的 ndarray 行

javascript - 如何在js中用正斜杠替换反斜杠

c++ - 如何编写用于html解析的正则表达式?

javascript - JavaScript 中的后向查找。词后空格

regex - 匹配最长的重复序列(不是由重复序列组成)

Python2.6 回溯/Ubuntu wxPython

python - 我应该直接使用python魔术方法吗?

python - 如何使用Python获取Excel工作簿的路径

regex - 具有特定长度的复杂正则表达式模式