0

There is a text with IP addresses on each line as well arbitrary ones with digits/dots. How can I match only lines that are not IP addresses?

10.123.34.12


asdADSas 3242 .

10.123.34.12

Empty lines are OK.

How do I solve this problem?

Emma
  • 1
  • 9
  • 28
  • 53
Safety1st
  • 23
  • 3

1 Answers1

2

While looking for NOT things is something RegEx can do, it's not something that it should do. Look-aheads can be much slower than just checking each line for a match and copying that match to another array/list/string. Depending on the accompanying language this could bottleneck your process.

That being said the RegEx you are looking for is:

^((?!\b((?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?:(?<!\.)\b|\.)){4}).)*$

You could also modify this to allow empty lines if you want.

Demo

Community
  • 1
  • 1
Bradd
  • 196
  • 10
  • Could you explain how that part works? `(? – Safety1st May 06 '19 at 22:39
  • If you go to the demo, it will actually break down each part of the regex individually. The specific part you mentioned is a negative look back asserting that I don't find a word boundary `\b`. – Bradd May 07 '19 at 15:46
  • Thank you for answer. Actually I used the demo a lot and tried to understand without success. It is inconvinient there is no capturing group so I'm not able to see how it works step by step. Could you reveal more details? – Safety1st May 07 '19 at 23:34