Splunk Search

How to edit my regular expression to extract a string between percentages and other characters?

jjmel
Explorer

I have to get "THIS" out of O_name%253DTHIS%2526, for my_field.

I'm a regex newb.

i tried the following but it is not working:

|rex field=_raw "O_name%253D(?P<my_field>[^\s]+)%2526"
0 Karma
1 Solution

govindsinghrawa
Path Finder

Your answer should work but alternatively you can try as below:

O_name%\d{3}[A-Za-z](?P< my_field >[^\%]+)

where the string starts with O_name%
then waits for 3 digits with \d{3}
then an alphabet A to Z or small case a to z with [A-Za-z]
then the field extraction (?P< my_field >[^\%]+) to stop reading at a %
then a + to match non % as many times till you hit a %

NOTE: Added space between < and my_field and > as the formatting on the website wasn't allowing without space <.my_field>

View solution in original post

govindsinghrawa
Path Finder

Your answer should work but alternatively you can try as below:

O_name%\d{3}[A-Za-z](?P< my_field >[^\%]+)

where the string starts with O_name%
then waits for 3 digits with \d{3}
then an alphabet A to Z or small case a to z with [A-Za-z]
then the field extraction (?P< my_field >[^\%]+) to stop reading at a %
then a + to match non % as many times till you hit a %

NOTE: Added space between < and my_field and > as the formatting on the website wasn't allowing without space <.my_field>

jjmel
Explorer

i got an error with this. mine returns a value, but it isnt the THIS value. it returns a big chunk of the event that i dont want, maybe because it looks for the very last instance of "%2526" to cut it off, when it should look for the first "%2526" right after "O_name%253D." there are other values besides O_name, like O_route, O_product that follow the same pattern. So the O_* part is kind of a delimiter that is specific to each value.

0 Karma

lukejadamec
Super Champion

I think you might have made a mistake.... I created some events, and tested this and it works.

|rex field=_raw "O_name%\d{3}[A-Za-z](?P< TheName>[^\%]+)" |table TheName

Don't forget to remove the space in the regex in the brackets before TheName
If it is not working for you then perhaps you should post some actual events.

0 Karma

jjmel
Explorer

youre right. my apologies to govindsinghrawat. answer accepted. thanks, all!!

0 Karma

lukejadamec
Super Champion

Is 'THIS' always after % 3 digits and a letter, and does 'THIS' always end with a %?

0 Karma

jjmel
Explorer

THIS always comes after "O_name%253D" and before "%2526," and there are other values besides O_name, like O_route, O_product that follow the same pattern. So the O_* part is kind of a delimiter that is specific to each value.

0 Karma

lquinn
Contributor

Have you got an example of the full event? I can't see anything immediately wrong with this ...

0 Karma

jjmel
Explorer

gave some more context below

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

[Puzzles] Solve, Learn, Repeat: Character substitutions with Regular Expressions

This challenge was first posted on Slack #puzzles channelFor BORE at .conf23, we had a puzzle question which ...

Splunk Community Badges!

  Hey everyone! Ready to earn some serious bragging rights in the community? Along with our existing badges ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...