Splunk Search

How to use Regex inside a Case statement?

amitrinx
Explorer

Hi,
How can i write this statement

| eval protocolUsed = case( regex consumerkey="[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}","O1",
regex consumerkey="^[a-z0-9A-Z]{2,}$", "O2"))

Labels (3)
0 Karma

ITWhisperer
SplunkTrust
SplunkTrust
| eval protocolUsed = case(match(consumerKey,"[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}"),"O1", match(consumerKey,"^[a-z0-9A-Z]{2,}$"), "O2")
0 Karma

amitrinx
Explorer

@ITWhisperer 
I am getting Error in 'EvalCommand': The expression is malformed. Expected ).

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Please check that you have copied it correctly because this has been copied directly from a valid search

0 Karma

amitrinx
Explorer

I want to match certain keys and group them as O1 and others set of Keys to O2, and then use the fields
If i have to use rex field then in that case I should create two fields?

Have you understood?

0 Karma

isoutamo
SplunkTrust
SplunkTrust

Yes I think that this is the easiest way to do it. Later you can use e.g. coalesce to select which value you have in current event. Or use case with isnull/isnotnull conditions.

0 Karma

isoutamo
SplunkTrust
SplunkTrust

Hi

What issue you are trying to solve?

regex command select rows which are matching it and drop others. So you cannot use it like this.

If you want to pick part of event to a new field then you should use rex command not regex.

r. Ismo

0 Karma
Get Updates on the Splunk Community!

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...

New Articles from Academic Learning Partners, Help Expand Lantern’s Use Case Library, ...

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

Your Guide to SPL2 at .conf24!

So, you’re headed to .conf24? You’re in for a good time. Las Vegas weather is just *chef’s kiss* beautiful in ...