When performing regex search within the array, it returns as empty

Viewed 43

When performing regex search within the array, it returns as empty

SCRIPT

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)

OUTPUT

[]
[]

REGEX101

1 Answers

Python re does not support \K construct.

It seems you do not even need it since all you need is the capturing group 1 values. Use

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)

See the Python demo. Output:

date45
date48ccc
Related