Splunk Search

How to create Rex for extracting a value from string?

Coder1a
Loves-to-Learn

Hello, I am new to splunk rex, need help for below to extract a value from string.

rex "Error while calling database for id = (?<id>.*)"

Example string: "Error while calling database for id =8748723874_1"

Output should be 8748723874

Thanks.

Labels (1)
0 Karma

yuanliu
SplunkTrust
SplunkTrust

You didn't show the result or explain why the result is wrong.  Do you mean you just want to get rid of the part after underscore _?  Normally, Splunk should have given you a field id that contains value 8748723874_1.  If so, always work from the smaller data.  You do not need regex for this task.  Just do

| eval id = mvindex(split(id, "_"), 1)

If Splunk doesn't give you this field, you can do

| rex "Error while calling database for id\s*=\s*(?<id>[^_]+)"

Hope this helps

0 Karma
Get Updates on the Splunk Community!

Technical Workshop Series: Splunk Data Management and SPL2 | Register here!

Hey, Splunk Community! Ready to take your data management skills to the next level? Join us for a 3-part ...

Spotting Financial Fraud in the Haystack: A Guide to Behavioral Analytics with Splunk

In today's digital financial ecosystem, security teams face an unprecedented challenge. The sheer volume of ...

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability

Solve Problems Faster with New, Smarter AI and Integrations in Splunk Observability As businesses scale ...