Splunk Search

rex - Extracting a string

jrowland1230
Explorer

I want to exact a string 'GUID" from the log right after "customers". This regex expression works in https://regex101.com/ but not in Splunk.  My field name is log:

2023-06-19 15:28:01.726 ERROR [communication-service,6e72370er2368b08,6e723709fd368b08] [,,,] 1 --- [container-0-C-1] c.w.r.acc.commservice.sink.ReminderSink : Reminder Message processed, no linked customers aaf60d69-99a9-41f5-a081-032224284066

 

| rex field=log "(?<cids>).*customers\s(.*)"

 

Labels (3)
0 Karma
1 Solution

P_vandereerden
Splunk Employee
Splunk Employee

Did you want cids to contain that GUID?

Try

| rex field=log ".*customers\s(?<cids>.*)"


Alternatively, if the GUID is always at the end, following a space, you can even drop the "customers" part:

| rex field=log "(?<cids>\S+$)"


Your example appears to be creating a capture group named "cids" that captures nothing (the first set of parentheses), and then a second non-capturing group that matches what you want (the second set of parentheses).
This document might help explain in more detail:
https://docs.splunk.com/Documentation/SCS/current/Search/AboutSplunkregularexpressions#Capture_group... 

Paul van der Eerden,
Breaking software for over 20 years.

View solution in original post

0 Karma

P_vandereerden
Splunk Employee
Splunk Employee

Did you want cids to contain that GUID?

Try

| rex field=log ".*customers\s(?<cids>.*)"


Alternatively, if the GUID is always at the end, following a space, you can even drop the "customers" part:

| rex field=log "(?<cids>\S+$)"


Your example appears to be creating a capture group named "cids" that captures nothing (the first set of parentheses), and then a second non-capturing group that matches what you want (the second set of parentheses).
This document might help explain in more detail:
https://docs.splunk.com/Documentation/SCS/current/Search/AboutSplunkregularexpressions#Capture_group... 

Paul van der Eerden,
Breaking software for over 20 years.
0 Karma

jrowland1230
Explorer

So to clarify the <cids> is the placeholder for the values produced from the regex AND also the placement is where the actual value would be contained in the string, i.e. Log field?

0 Karma

P_vandereerden
Splunk Employee
Splunk Employee

Yes. You can name multiple capture groups in one rex statement. 

e.g.

| rex field=my_field "foo:\s+\"(?<first_capture>[^\"]+)\",\s+bar:\s+(?<second_capture>[^\"]+)"
Paul van der Eerden,
Breaking software for over 20 years.
0 Karma

jrowland1230
Explorer

Thank you. I was close ugh.

Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...