Splunk Search

How to write regex to select specific fields on a search?

carlyleadmin
Contributor

hi below is my search,

when I do search for Error this is what I get;
alt text

then I run this search to create "Message" field and it brings up from ERROR to ***Exception message*

index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?.*)"|table host,Message,_time

I basically want to bring everything starting from ERROR to all the way down to the last line. How do I do that?

I also wanted to extract another field starting from **Exception message** to ***Stack Trace* but it doesn't work.any help is appreciated.

for that, I tried using multiple combinations and I was not successful. i tried using; |rex " Exception message (?.Stack trace)", |rex " *Exception message* (?.*Stack trace)", |rex " Exception message (?.*)",

I tried using field extractor to address the issue but I guess it was to complex that it kept giving me error while trying to extract it.

I've never written regex manually before so please be patient with me:)

0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

A most excellent resource for writing regular expressions is regex101.com. There you can paste some sample data then try various regex strings to see what matches.

To get Splunk to match text across lines, use [\s\S]* instead of .*. So your first search would become index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| table host,Message,_time.

---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

A most excellent resource for writing regular expressions is regex101.com. There you can paste some sample data then try various regex strings to see what matches.

To get Splunk to match text across lines, use [\s\S]* instead of .*. So your first search would become index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| table host,Message,_time.

---
If this reply helps you, Karma would be appreciated.

carlyleadmin
Contributor

Thank you richgalloway.i will definitely check regex101.

quick question if i wanted to extract 2 fields,first one would be the lines starting from Error to *Exception Message* and second would be the lines between **exception message to the the last line what would be the search look like.thanks for all the help

0 Karma

richgalloway
SplunkTrust
SplunkTrust

That's probably best done with two rex commands.

index=pa host=aptr45 sourcetype=Service| transaction startswith="Error" endswith="ERROR" maxevents=15000 | rex "ERROR (?<foo>[\s\S]*)"| rex "ERROR (?<bar>.*)" | table host,Message,_time
---
If this reply helps you, Karma would be appreciated.
0 Karma
Get Updates on the Splunk Community!

Index This | What goes away as soon as you talk about it?

May 2025 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this month’s ...

What's New in Splunk Observability Cloud and Splunk AppDynamics - May 2025

This month, we’re delivering several new innovations in Splunk Observability Cloud and Splunk AppDynamics ...

Getting Started with Splunk Artificial Intelligence, Insights for Nonprofits, and ...

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...