Splunk Search

use regex to remove a number from a string

matansocher
Contributor

Hi,

I want to remove a number (up to 5 digits) from a string on its beginning.
an example:

43.aaaa_vvvvv.cccccc:dddddd => aaaa_vvvvv.cccccc:dddddd
9374.aaaa_vvvvv.cccccc:dddddd => aaaa_vvvvv.cccccc:dddddd
1.aaaa_vvvvv.cccccc:dddddd => aaaa_vvvvv.cccccc:dddddd

I only need to remove the first number and the "." after it.

thanks

0 Karma
1 Solution

niketn
Legend

@matansocher, is this a field or raw data?
You can try the following rex command:

<your base search>
| rex field=_raw "\d+.(?<myData>.*)"
| table _raw myData

I have use field name as _raw but you can replace with your own if it is some other field. Alternatively you can also use replace() command with regular expression if this is a field.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"

View solution in original post

0 Karma

nikita_p
Contributor

Hi matansocher,
Try the regex below. Let me know if this works.
index=xyz| rex field=_raw "^(?P[^.]+)"

0 Karma

ryhluc01
Communicator

This produces this message:

"Error in 'rex' command: Encountered the following error while compiling the regex '^(?P[^.]+)': Regex: unrecognized character after (?P"

0 Karma

niketn
Legend

@matansocher, is this a field or raw data?
You can try the following rex command:

<your base search>
| rex field=_raw "\d+.(?<myData>.*)"
| table _raw myData

I have use field name as _raw but you can replace with your own if it is some other field. Alternatively you can also use replace() command with regular expression if this is a field.

____________________________________________
| makeresults | eval message= "Happy Splunking!!!"
0 Karma
Get Updates on the Splunk Community!

Index This | Why do they call it hyper text?

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

State of Splunk Careers 2023: Career Resilience and the Continued Value of Splunk

For the past three years, Splunk has partnered with Enterprise Strategy Group to conduct a survey that gauges ...

The Great Resilience Quest: 9th Leaderboard Update

The ninth leaderboard update (11.9-11.22) for The Great Resilience Quest is out &gt;&gt; Kudos to all the ...