Question

I'm working on some logs right now and I have a problem that I couldn't resolve yet.

I want to find with regular expression all the ips in each line in my log file.

My problem is, that I have something unique in this logs, called oid that most of the times is written like this (the numbers are an example): oid: 1.3.2.4.5.3.2.4.2.2.

As I watched in my logs, the numbers are various and sometimes I can get a lines like:

oid: 1.3.2.4.512.3.2.434.2.2.

As you can understand, my regular expression finds this oid (which is not an ip) and causes me a lot of problems.

Can someone give me a regular expression that will fix this problem?

** I used this regex to find ips in line: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

Thanks a lot.

Was it helpful?

Solution

You could use some lookarounds to help make sure there are no other . around the IP:

(?<!\.)\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b(?!\.\d)

(?<!\.) makes sure there's no dot before the IP and (?!\.\d) makes sure there's no . after the IP and a number. I added a number because I think you could have an IP followed by a dot if that last dot is meant to be the period of the end of a sentence.

The word boundaries (\b) prevent matching between numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top