Splunk Search

Help with ending search for rex expression

pushpender07
Explorer

Hi All - I am using the below query

index=ABC "XYZ"| rex field=_raw "\"code\":\"(?.*)\"" | stats count by errorcode

This gives me an output like this
SR123","message":"Added Successfully." 2
SR456","message":"Declined." 4

the error code I am trying to extract is SR123 or SR456. How do I ensure the rex command ends errorcode at the 2nd " and only gives count for SR123 and SR456. Please help as I have never used rex field in command. Tried few hit and trial scenarios, didn't work.

Tags (1)
0 Karma
1 Solution

somesoni2
Revered Legend

Give this a try (regex is starting to capture value after code":" till next double quote)

index=ABC "XYZ"| rex field=_raw "\"code\":\"(?<errorcode>[^\"]+)" | stats count by errorcode

View solution in original post

somesoni2
Revered Legend

Give this a try (regex is starting to capture value after code":" till next double quote)

index=ABC "XYZ"| rex field=_raw "\"code\":\"(?<errorcode>[^\"]+)" | stats count by errorcode

pushpender07
Explorer

Follow up question. If the output of the original query is like below

SR123","message":"Added Successfully.","error":null.

and I would want the output till
SR123","message":"Added Successfully."

Can I do that using rex? This is to help folks know what each message means.

0 Karma

thambisetty
SplunkTrust
SplunkTrust

message will extract after code":" to till last comma in a row.

index=ABC "XYZ"| rex field=_raw "\"code\":\"(?<message>.+)," 
————————————
If this helps, give a like below.
0 Karma

pushpender07
Explorer

perfect, it worked. Can you please explain what did you change do logically?

0 Karma

macadminrohit
Contributor

also you can use regex101.com , it helps a lot to build the regex.

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!

Painting a Clearer Picture: Creating Cross-Domain Visibility with AI Canvas

    Thursday, June 25, 2026  |  11AM PDT / 2PM EDT  Duration: 1 Hour (Includes live Q&A) Register to ...

Analytics Workspace deprecation

As of Splunk Cloud Platform 10.4.2604 and Splunk Enterprise 10.4, Analytics Workspace is now deprecated. ...

Splunk Developer Day Recap: Building, Publishing, and Growing on the Splunk Platform

Splunk Developer Day brought the Splunk developer community together for a practical look at what it means to ...