Splunk Search

Regex help! (easy one)

beaunewcomb
Communicator

I need to extract fields from a set of results with inconsistent formatting. I think this would be easy for a regex pro.

Here are two events:

Jul 17 15:44:01 hostname 192.168.0.1 [st2-b3-inter-d005][system][error] trans(119657588)[192.168.0.2]: Unable to open URL

Jul 17 15:44:01 hostname 192.168.0.1 [network][error] trans(2064751791): Host connection could not be established

I need multiple extractions for the data within the first set of brackets (separated by the dash) in event 1. You'll see that event 2 doesn't contain this type of data at all.

Basically I need a regex that says "Match everything between the first [ and first - but not if there are more than 3 characters before the -"

I'm basically a regex noob!

Thanks!

Tags (1)
0 Karma
1 Solution

Ayn
Legend

This should do it:

\[([^-]{0,3})-

View solution in original post

Ayn
Legend

This should do it:

\[([^-]{0,3})-

beaunewcomb
Communicator

Hahah you helped me!

You rock.. thanks a lot. I need to get this regex stuff down.

0 Karma

Ayn
Legend

I did nothing to you! 😉

No, sure. The regex first looks for the opening bracket, which has to be escaped because [ is a special character in regexes otherwise.

\[

Then the matching starts. We're looking for characters that are NOT the dash sign.

\[([^-]

Match if we find at least 0 and at most 3 non-dash characters.

\[([^-]{0,3}

End our matching group, and only match if this is immediately followed by a dash sign.

\[([^-]{0,3})-

I hope that sheds some light on how the regex is built step by step.

beaunewcomb
Communicator

Is there any way you could explain what you did to me? Because I have to build extractions for the data in the separate sections (separated by -) in that bracket.

Awesome, fast answer btw.

0 Karma

Ayn
Legend

The field extractor wants a named extraction for FIELDNAME if I remember correctly, so:

\[(?P<FIELDNAME>[^-]{0,3})-

beaunewcomb
Communicator

Wow, fast response! How would I use that in the field extractor?

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!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...