Splunk Search

RegEx to Find First Match of OR

chrisboy68
Contributor

Hi, banging my head...

04/22/2016 09:23:50,865 - ERROR - exception occurred --- FOO BAR
Severity:        Error
Message:         Timeout expired. 
MachineName:     MY MACHINE


04/22/2016 09:23:56,318 - ERROR - exception occurred --- System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

I have a single log file that contains the two formats. What I want to do is if the entry has "Message:", then make this the extracted field ErrorMessage, Else If the entry have "ERROR" then, make this the extracted field ErrorMessage.

(?:Message:)|(?:ERROR -)|\s+(?P(?:[^\n]*)) this is the regex I started with and modified with few things and I still can't get it right.

Help appreciated.

Thank you,

Chris

Tags (2)
0 Karma
1 Solution

ktugwell_splunk
Splunk Employee
Splunk Employee

You could do 2 field extractions both producing the same field name ErrorMessage

props.conf

[yoursourcetype]
EXTRACT-1 = Message:\s+(?<ErrorMessage>[A-Za-z0-9 ]+)
EXTRACT-2 = \d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2},\d{3}\s-\sERROR\s-\s(?<ErrorMessage>.+)

That regex isn't the best in the world but you can keep tweaking it to suit your needs

Be sure to add all the possible characters you may see after "Message:" in this part of the regex [A-Za-z0-9 ]

View solution in original post

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

You could do 2 field extractions both producing the same field name ErrorMessage

props.conf

[yoursourcetype]
EXTRACT-1 = Message:\s+(?<ErrorMessage>[A-Za-z0-9 ]+)
EXTRACT-2 = \d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}:\d{2},\d{3}\s-\sERROR\s-\s(?<ErrorMessage>.+)

That regex isn't the best in the world but you can keep tweaking it to suit your needs

Be sure to add all the possible characters you may see after "Message:" in this part of the regex [A-Za-z0-9 ]

0 Karma

chrisboy68
Contributor

Thanks, but wouldn't that still execute both EXTRACTs? I just want One or the Other.

Chris

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

Give it a try, I may be wrong, but I think you'll only only see EXTRACT-2 as ErrorMessage if EXTRACT-1 doesn't exist. Either that or it'll create a multi value field.

0 Karma

chrisboy68
Contributor

Thanks. You look to be correct! Its working.

Chris

0 Karma

ktugwell_splunk
Splunk Employee
Splunk Employee

Good stuff!

0 Karma
Get Updates on the Splunk Community!

Accelerating Observability as Code with the Splunk AI Assistant

We’ve seen in previous posts what Observability as Code (OaC) is and how it’s now essential for managing ...

Integrating Splunk Search API and Quarto to Create Reproducible Investigation ...

 Splunk is More Than Just the Web Console For Digital Forensics and Incident Response (DFIR) practitioners, ...

Congratulations to the 2025-2026 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...