Splunk Search

Regex for extraction

xvxt006
Contributor

Hi,

i want to extract account field and i have events in 2 patterns. One where account has boundaries of @account= and right boundary as & as shown in event 1

2nd pattern is having @account as left boundary and left boundary as space

Event1 pattern is working fine but Event2 gives the whole event after account= which is 123452 HTTP/1.1" 200 1216 109354 "-" "-" "-"

What should i have just to have the account value from Event2 pattern

Event1
?product=%20FILTERS%2016z20r2&account=12334312&country=US HTTP/1.1" 200 3388 218391 "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "

Event2
?product=2CSY1&account=123452 HTTP/1.1" 200 1216 109354 "-" "-" "-"

Expression used:
(?i)&account=(?P[^&]+)(?:\s|[&].*)

Tags (1)
0 Karma
1 Solution

kyleharrison
Path Finder

This should work:
(?i)&account=(?P[^&\s]+)

Tested ok on regex101.com with the two events you gave.

View solution in original post

kyleharrison
Path Finder

This should work:
(?i)&account=(?P[^&\s]+)

Tested ok on regex101.com with the two events you gave.

kyleharrison
Path Finder

Then you can change the "+" to "*" to match 0 or many characters ("+" matches 1 or many):

(?i)&account=(?P[^&\s]*)

That would produce: account=""

If you wanted it to be "-", then you can use this eval:

In search:
| eval account=if(isNull(account),"-",account)

In props.conf:

EVAL-account = if(isNull(account),"-",account)

0 Karma

xvxt006
Contributor

One question some times it has no value in it. as shown below. i was expecting something like - in the captured values. if i want to capture that as well, can i do that?
?product=%20FILTERS%2016z20r2&account=&country=US HTTP/1.1" 200 3388 218391

0 Karma

xvxt006
Contributor

Thank you. It worked.

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

Tech Talk Recap | Mastering Threat Hunting

Mastering Threat HuntingDive into the world of threat hunting, exploring the key differences between ...

Observability for AI Applications: Troubleshooting Latency

If you’re working with proprietary company data, you’re probably going to have a locally hosted LLM or many ...

Splunk AI Assistant for SPL vs. ChatGPT: Which One is Better?

In the age of AI, every tool promises to make our lives easier. From summarizing content to writing code, ...