Splunk Search

REX command: something or something or nothing

willial
Communicator

Here's my rex:

rex max_match=0 "(MSM-\w+\s+(?<slotMSM>\w+)\s+|MM-\w+\s+(?<slotMM>\w+)\s+|Slot-\d+\s+(?<slotNum>\d+)\s+|)OtherStuffAndsoOn"

This is at the front of a longer bit of rex, so basically a line could start with MSM-A or MM-A or Slot-1, or it could just start at the "OtherStuffAndSoOn" part where there's additional rex that's working fine. I'm trying to accomplish this by ending the first set of OR statements with |), indicating that I'd like it to consider "or none of these."

The problem is that it will pick up lines that start with MSM or MM or Slot, but won't pick up the ones that don't. The |) bit appears to do nothing.

I need to keep it max_match=0 as one log could contain any number of lines (in any of the above configurations) that need to be extracted.

Tags (1)
0 Karma
1 Solution

Ayn
Legend

You can probably achieve what you want by putting a '?' or '*' after your whole regex. Both these will make the preceding match optional.

http://www.regular-expressions.info/optional.html

View solution in original post

Ayn
Legend

You can probably achieve what you want by putting a '?' or '*' after your whole regex. Both these will make the preceding match optional.

http://www.regular-expressions.info/optional.html

willial
Communicator

I discovered that the problem was actually in an eval/mvzip later in the search coming up blank due to the above rex being empty, and requiring an if isnotnull to suss out whether or not it should be zipped, followed by the zip extraction requiring an OR to cover the possibility of there being nothing to extract there.

Either way, this answer works. Also, the (blah\blah\blah|) construction appears to work fine. Everything works, just not my brain. Thanks!

somesoni2
Revered Legend

like this
rex max_match=0 "(MSM-\w+\s+(?\w+)\s+|MM-\w+\s+(?\w+)\s+|Slot-\d+\s+(?\d+)\s+)*OtherStuffAndsoOn"

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!

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...

Skip the Awkward Silence: Have a .conf-ersation at .conf26

Picture this. You arrive at .conf26 already having your socializing and networking plans mapped out. No ...

Rethinking Zero Trust: From Product Purchases to Logical Control Evidence

Implementing Zero Trust (ZT) across complex environments often falters at the very beginning due to a ...