Splunk Search

Help with regex

bosseres
Contributor

Hello everyone,

I need help with regex

I have search

index=*
| regex Commandline="my_regular_expression"

How can I add one more regular expression with OR condition?

something like this | regex Commandline="my_regular_expression" OR | regex Commandline="my_regular_expression2"

 

Tahnk you

Labels (2)
0 Karma

bosseres
Contributor

Thank you very much for responding, and what if I have such condition?

| regex Commandline="my_regular_expression" | (regex Commandline="my_regular_expression2" AND regex Commandline="my_regular_expression3" )


(AND added)

0 Karma

javiergn
Super Champion

Hi again,

I couldn’t say. There are many ways to do it. If you can be a bit more specific about your regex I might be able to help more. 

Translating Boolean operators to regex is not trivial and it is not what regex was designed for originally. 

You can replicate an OR the way I told you earlier. You can replicate an AND by simply doing a second regex after the first one but also within the same regular expression assuming the text you want to match follows that order. 

In summary, if you can give us an example of what you are trying to match we could probably help you a lot more. 

bosseres
Contributor

I have 3 regex expressions - regex1, regex2, regex

and I need to find events where field Commandline matches  regex1 or matches regex2 AND regex3

regex1 OR (regex2 AND regex3)

is it possible? thank you

0 Karma

javiergn
Super Champion

It will look like the following:

index=YOURINDEXHERE
| regex Commandline="(regex1|regex2.*regex3)"

 

But I could be wrong as I still don't know what sort of regex you want to write or what kind of string you want to match. Hope it helps anyway.

 

javiergn
Super Champion

Hi @bosseres ,

You could try with the following:

index=*
| regex Commandline="(keyword1|keyword2)"

 

But if you can tell us exactly what sort of regex you want to implement we might be able to assist a bit more.

Regards,

Javier

aasabatini
Motivator

Hi @bosseres 

 

the OR condition on regular expression is the pipe "|",

aasabatini_1-1639648449355.png

 

 

I show you an example

rex filed=_raw "\w+\s+(?<name_field>\w+|\w+\d+)"
“The answer is out there, Neo, and it’s looking for you, and it will find you if you want it to.”
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...