Splunk Search

How to create a regex to extract the first IP address from multiple IP addresses in an event line?

reverse
Contributor

There are multiple ip addresses in a raw event line and I only need the first one
How can I achieve that?

192.168.0.1 -192.168.0.5 gcgh 192.168.0.7gcgh 192.168.0.7 gcgh 192.168.0.8 gcgh 192.168.0.9 gcgh
Tags (2)
0 Karma
1 Solution

felipesewaybric
Contributor

Something like that:

([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/*\d*).+

View solution in original post

0 Karma

jazzypai
Path Finder

Adding context for anyone coming here for answers.

Assuming msg is the field name, the following would result in the first IP found

| makeresults  
| eval msg = "192.128.22.2 202.134.55.89" 
| rex field=msg "(?<firstip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"

If additional IPs needed to be extracted then a max_match = # could be added to the rex line.

felipesewaybric
Contributor

Something like that:

([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/*\d*).+
0 Karma

reverse
Contributor

worked by removing .+ at the end ...

0 Karma

reverse
Contributor

not working .. i tried here
https://regex101.com/r/2MblRX/1

0 Karma
Get Updates on the Splunk Community!

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

Splunk is officially part of Cisco

Revolutionizing how our customers build resilience across their entire digital footprint.   Splunk ...

Splunk APM & RUM | Planned Maintenance March 26 - March 28, 2024

There will be planned maintenance for Splunk APM and RUM between March 26, 2024 and March 28, 2024 as ...