Splunk Search

How to remove a word with Regex using Capturing group?

poojithavasanth
Explorer

Hello,

I have a Regex for splitting a Person full name into Person lastname, firstname and middlename.

Regex used: (?<prsnl_last>\w+)([\s]*(?<prsnl_credentials>[\w]*)|)[\s]*,[\s]*(?<prsnl_first>\w+)([\s]*(?<prsnl_middle>[\w]+.*?)|)

poojithavasanth_1-1674138466518.png

 

Now I would need to remove prsnl_last from the output. (Basically, to scrub the data from prsnl_last event)

Output should be something like this. "Haikal" and "Campbeli" should be removed.

poojithavasanth_2-1674138919979.png

Can someone please help me out?

Thank you!

Labels (1)
Tags (2)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Can you declare it as a non-capture group?

(?:\w+)([\s]*(?<prsnl_credentials>[\w]*)|)[\s]*,[\s]*(?<prsnl_first>\w+)([\s]*(?<prsnl_middle>[\w]+.*?)|)
0 Karma

poojithavasanth
Explorer

This Regex works! Thank you @ITWhisperer 

Just checking if we can create a Regex using a capturing group<prsnl_last> and then to remove the data within it?

So that the first word assigns to <prsnl_last>, but the value would be null.

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try something like this

(?:\w+)(?<prsnl_last>)([\s]*(?<prsnl_credentials>[\w]*)|)[\s]*,[\s]*(?<prsnl_first>\w+)([\s]*(?<prsnl_middle>[\w]+.*?)|)

Here is an example using rex

| makeresults 
| eval name="Kirk Captn, James T"
| rex field=name "(?:\w+)(?<prsnl_last>)([\s]*(?<prsnl_credentials>[\w]*)|)[\s]*,[\s]*(?<prsnl_first>\w+)([\s]*(?<prsnl_middle>[\w]+.*?)|)"
0 Karma

poojithavasanth
Explorer

Thaks for the inputs. 

I tried using the provided Regex; however it picks up space as the prsnl_last and then does not give any value in that column. Can we have a regex which picks up prsnl_last name only and then removes it while displaying them?

poojithavasanth_1-1674451391998.png

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

It sounds like you have two steps, one to extract the fields and another to display them. You can either extract the field with a value (as in my first suggestion), or without a value (as in my latest suggestion). When you come to display it in dashboard, you could change the value of the field before displaying it (eval) or remove the field from the field list (fields).

0 Karma
Get Updates on the Splunk Community!

Everything Community at .conf24!

You may have seen mention of the .conf Community Zone 'round these parts and found yourself wondering what ...

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...

New Articles from Academic Learning Partners, Help Expand Lantern’s Use Case Library, ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...