Splunk Search

How to create a regex that removes everything before the second underscore?

gbwilson
Path Finder

I'm trying to create a regex that removes everything before the second underscore in a string. The number of characters before the second underscore varies.

For example:

DR300_Corp_76
BELLOE_MX400_32
AB200_Corp_123

I only want the two or three digits after the second underscore (i.e. 76, 32, 123, etc.)

Tags (2)
0 Karma
1 Solution

somesoni2
Revered Legend

If the end of you string is always numbers, try like this

your current search 
| eval yourfield=replace(yourfield,".+_(\d+)$","\1")

if not, try this

your current search 
| eval yourfield=replace(yourfield,"^([^_]+_){2}(.+)$","\2")

View solution in original post

0 Karma

somesoni2
Revered Legend

If the end of you string is always numbers, try like this

your current search 
| eval yourfield=replace(yourfield,".+_(\d+)$","\1")

if not, try this

your current search 
| eval yourfield=replace(yourfield,"^([^_]+_){2}(.+)$","\2")
0 Karma

gbwilson
Path Finder

Thanks for your help! First one worked great.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi gbwilson,
try this

(?<my_field>[^_]*_[^_]*)_.*

test it in https://regex101.com/r/YHRXW9/1
Bye.
Giuseppe

0 Karma
Get Updates on the Splunk Community!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...