-1

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().

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Jamie
  • 1
  • 1
  • 1
    Any regex that matches only IPs won't match non IPs. EDIT: Ah, you want to do a search and replace of IPs within text. I'm still confused why any regex that matches only IPs would ever match `-`. – Mateen Ulhaq Jun 07 '20 at 16:36
  • You get partial matches as the are no boundaries like anchors or word boundaries `\b` Note that in this part `[.| ]` the pipe matches literally a pipe char and not OR `\b\d{1,3}[. ]\d{1,3}[. ]\d{1,3}[. ]\d{1,3}\b(?!-)` See https://regex101.com/r/0gQ130/1 – The fourth bird Jun 07 '20 at 16:37
  • `ipre = re.compile(r'(?<!\d)\d{1,3}[. ]\d{1,3}[. ]\d{1,3}[. ]\d{1,3}(?![-\d])')` – Wiktor Stribiżew Jun 07 '20 at 17:18
  • Mateen Ulhaq, The Fourth Bid & Enrico Maria De Angelis - thank you for your replies. @WiktorStribiżew - thank you also, and apologies for missing your earlier answer. I appreciate the help. – Jamie Jun 07 '20 at 18:06

1 Answers1

0

The question is not clear yet, so this answer could need some edits as soon as you'll provide enough details.

The point is the input file you shared has only one IP (with or without a dash at the end), on each line. So you can just improve the IP-matching regex by requiring the the line only contain that, by prepending and appending the anchors ^ and $ respectively:

^\d{1,3}[. ]\d{1,3}[. ]\d{1,3}[. ]\d{1,3}$

See it live on regex101.

Enlico
  • 23,259
  • 6
  • 48
  • 102