Splunk Search

using rex to extract from json

gds506
New Member

Hi,
I'm working on a akamai json and I want to extract the OS name from the message.UA field. Basically, if you look at the fake sample string below, I only want to get Windows (the part of the string between the first ( and the next %:

Mozilla%2f5.0%20(Windows%20NT%2018.0%3b%20Win64%3b%20x64)%20AppleWebKit%2f580.36%20(KHTML,%20like%20Gecko)%20Chrome%2f81.0.4042.140%20Safari%2f537.36

I already created a regular expression that will do exactly what I want but I'm not able to make it work with rex (As you can imagine I'm new in Splunk). Here is how I'm trying to use it.

index=akamai | regex field = message.UA "(?<=\()(.*?)(?=\%)"| top message.UA

When I run it I get a

Error in 'rex' command: The regex
'(?<=()(?=\%)' does not extract
anything. It should specify at least
one named group. Format: (?...).

Any idea of how to accomplish this extraction?

Thanks!

0 Karma

darrenfuller
Contributor

Hi GDS...

you asked about REX, but in the example given you have used regex (but then used the syntax for rex... so i am going to answer with rex..

Here is a run anywhere example that answers (i think) your requirements.

| makeresults | fields - _time
| eval message.UA="Mozilla%2f5.0%20(Windows%20NT%2018.0%3b%20Win64%3b%20x64)%20AppleWebKit%2f580.36%20(KHTML,%20like%20Gecko)%20Chrome%2f81.0.4042.140%20Safari%2f537.36"
| rex field=message.UA "^[^\(]+\((?<OS>[^\%]+)\%"

in your regex... you don't have a named capture group: ie... (?PATTERN) ... that's what the system is complaining about in the error stated above

0 Karma
Get Updates on the Splunk Community!

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...

Welcome to the Splunk Community!

(view in My Videos) We're so glad you're here! The Splunk Community is place to connect, learn, give back, and ...