Splunk Search

Help with regex on string with multiple number values

morgantay96
Path Finder

Hi all need help getting the trailing number from a field in a search.

Examples of the field

id = bdf73ad5-4499-4f70-b7e3-e2c81ae868c3-default-asset-423447

id = bdf73ad5-4499-4f70-b7e3-e2c81ae868c3-default-asset-6672

id = bdf73ad5-4499-4f70-b7e3-e2c81ae868c3-default-asset-4232323

I was using....

 

 

 

 

| eval stripped_asset_id=substr(id, -6)

 

 

 

 

  however that only is consistent if the last numbers consist of 6 digits which it often may have more or less.

How can I take everything after the last dash "-"?

Labels (1)
0 Karma
1 Solution

jnorha
Engager

Try adding this pipe:

 

| rex field=id "(?<new_field_name>[^\-]*$)"

 

This will run a lookbehind from the last dash in the end of the string and create a new field with your new field name. In this case id is a field and the full string is that fields value

View solution in original post

gcusello
SplunkTrust
SplunkTrust

Hi @morgantay96,

you could also try this:

| rex field=id "^([^\-]*\-){7}(?<your_field>\d+)"

that you can test at https://regex101.com/r/dDfGBk/1

Ciao.

Giuseppe

0 Karma

jnorha
Engager

Try adding this pipe:

 

| rex field=id "(?<new_field_name>[^\-]*$)"

 

This will run a lookbehind from the last dash in the end of the string and create a new field with your new field name. In this case id is a field and the full string is that fields value

isoutamo
SplunkTrust
SplunkTrust

And if you want to optimise that regex then you can use this if all your id:s have above format.

| rex field=id "t-(?<new_field_name>\d+$)"

https://regex101.com/r/2qGsRf/1 (3 matches 33 steps)

vs. original

 https://regex101.com/r/bhW62a/1 (6 matches 1 354 steps)

No mater if you have only couple of events, but when there are millions or more then this can be meaningful difference.

r. Ismo

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Take Action Automatically on Splunk Alerts with Red Hat Ansible Automation Platform

 Are you ready to revolutionize your IT operations? As digital transformation accelerates, the demand for ...

Calling All Security Pros: Ready to Race Through Boston?

Hey Splunkers, .conf25 is heading to Boston and we’re kicking things off with something bold, competitive, and ...

Beyond Detection: How Splunk and Cisco Integrated Security Platforms Transform ...

Financial services organizations face an impossible equation: maintain 99.9% uptime for mission-critical ...