There are a lot of IP regex questions. Hopefully I've not missed the one where this is answered.
I would like to match IP address style patterns which are joined with with either a dot or space, but not when the last 'octet' is followed by a hyphen. I don't need the 'addresses' to be valid IP addresses.
Match:
10.10.10.110
678.160.1.1
10 10 10 110
010 160 1 12
Don't match:
10.10.10.110-
192.160.1.1-
10 10 10 110-
333 160 1 1-
I would like to replace either type of matching addresses with:
X.X.X.X
This is close:
ip = '192.168.10.111-'
ipre = re.compile(r'\d{1,3}[.| ]\d{1,3}[.| ]\d{1,3}[.| ]\d{1,3}(?!-)')
re.sub(ipre, 'X.X.X.X', ip)
But it only really works when the final octet has one digit.
I would be grateful for some help.
NB: I need to keep using re.sub()
.