How to handle grok optional pattern (?:)

Hi everyone I'm new to Elastick Stack, I set grok custom pattern using RegEx:
MODULE_NAME (?:((?<=[\[])\/\S[^\]]+))
the problem is it's optional pattern so I want the "module_name" field to be empty instead of showing "_grokparsefailure" tag
logstash filter:

grok{
    patterns_dir => ["./patterns"]
    match => {
        "message" => '%{DATETIMECATALINACUSTOMFR:logtimestamp}%{SPACE}%{JAVACLASS:javaclass}%{SPACE}%{WORD:method}%{NEWLINE}%{GREEDYDATA:exception}'
        }
}
if[exception]{
    grok{
        patterns_dir => ["./patterns"]
        match => {
            "exception" => '%{MODULE_NAME:module_name}'
        }
    }
}

Hi

Optional pattern is not ?: look "?:" meaning in grok · Issue #42 · GitHub for more informations.
To make a field optional you have to do ()?

So in your case it is MODULE_NAME ((?<=[\[])\/\S[^\]]+)?

Cad.

1 Like

thanks Cad for your replay, I already tested that pattern with grok debugger and ruby before asking
the problem in this case (pattern)? is module_name field alwayse return empty ""
in two cases, when [/whatever] exist and not

What is the look of the data you want to get ? can you share us example ?
Because when you share us the value [/watever] i understand that only watever can change. Is that true ?

Cad.

I mean [/watever] may or not exist in log files
so I want to store "whatever" or "/whatever " in new field
if it's exists in log files

Then something like this should work :

([\[\/](?<content>[^\]]*)[\]])?

It gonna store the value in content field.

Cad.

1 Like

The problem is not the pattern
The problem is when I put it between ()? it always return empty string ""

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.