Skip to content
Advertisement

Pattern matching for log analysis

My program will issue a grep command to search the log base on time range and a unique key word. My program able to issued out the grep command successfully and it’s return several matched line of log which look like the following:

JavaScript

But I don’t need all of this, the things I am interested in is [remoteAddress=/172.16.8.1:64931]. This line of code Pattern pat1 = Pattern.compile("remoteAddress=/(d)"); giving the illegal escape character. May I know how to extract out the IP address only without any port number and store it into a String variable, I had searched some information on google but it fails to work.

For your reference, this is my source code:

JavaScript

Advertisement

Answer

This regex matches digits and dots after remoteAddress=/ phrase.

JavaScript

It won’t match remoteAddress=STEDGE/172.16.8.3.

It uses positive lookbehind to assert that (?<=remoteAddress=/) is before 172.16.8.1

Pattern:

(?<=remoteAddress=/) positive lookbehind(zero-length assertion). It matches only if [\d.]+ is preceded by exact phrase remoteAddress=/.

[\d.]+ match digit or period . for 1 or more times. Doesn’t match anything else.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement