Splunk Search

regex fu (if contains : do this)

nickhills
Ultra Champion

Hey guys, this is one for any regex grand masters.

I have a field (snort_dst) which contains addresses in both these formats:

1.2.3.4
5.6.7.8:910

where :910 denotes the port number.
I have the following search, which splits the address into snort_dst_ip and snort_dst_port fields:

| rex field=snort_dst "(?<snort_dst_ip>[^:]+):(?<snort_dst_port>\S+)" 

This properly splits on the : but my values which are just IPs don't get matched, as they don't fit the :portnum regex.

What I need is something which will work in either situation, something like "if contains : do this, else do this", but my regex knowledge is still in its infancy.

If my comment helps, please give it a thumbs up!
Tags (2)
0 Karma
1 Solution

sowings
Splunk Employee
Splunk Employee

Put the section with a colon and the port in a group that you'll declare is optional:

(...)?

Try this.

| rex field=snort_dst "(?<snort_dst_ip>[^:]+)(:(?<snort_dst_port>\S+))?"

View solution in original post

sowings
Splunk Employee
Splunk Employee

Put the section with a colon and the port in a group that you'll declare is optional:

(...)?

Try this.

| rex field=snort_dst "(?<snort_dst_ip>[^:]+)(:(?<snort_dst_port>\S+))?"

nickhills
Ultra Champion

Thanks sowings, thats exactly what I needed. I knew it would be simple!

If my comment helps, please give it a thumbs up!
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...