# How to handle grok optional pattern (?:)

**URL:** <https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796>\
**Category:** Logstash\
**Created:** [May 3, 2022, 3:30am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796 "2022-05-03T03:30:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![marwen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marwen/32/105159_2.png) [@marwen](https://discuss.elastic.co/u/marwen)\
**Post date:** [May 3, 2022, 3:30am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/1 "2022-05-03T03:30:06Z")

</div>

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:

```auto
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}'
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![Cad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cad/32/86661_2.png) [@Cad](https://discuss.elastic.co/u/Cad)\
**Post date:** [May 3, 2022, 9:30am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/2 "2022-05-03T09:30:57Z")

</div>

Hi

Optional pattern is not `?:` look ["?:" meaning in grok · Issue #42 · GitHub](https://github.com/logstash-plugins/logstash-filter-grok/issues/42) for more informations.  
To make a field optional you have to do `()?`

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

Cad.

---

<div class="post-metadata">

**Author:** ![marwen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marwen/32/105159_2.png) [@marwen](https://discuss.elastic.co/u/marwen)\
**Post date:** [May 3, 2022, 11:56am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/3 "2022-05-03T11:56:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Cad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cad/32/86661_2.png) [@Cad](https://discuss.elastic.co/u/Cad)\
**Post date:** [May 3, 2022, 12:13pm UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/4 "2022-05-03T12:13:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![marwen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marwen/32/105159_2.png) [@marwen](https://discuss.elastic.co/u/marwen)\
**Post date:** [May 3, 2022, 12:16pm UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/5 "2022-05-03T12:16:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Cad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cad/32/86661_2.png) [@Cad](https://discuss.elastic.co/u/Cad)\
**Post date:** [May 3, 2022, 1:01pm UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/6 "2022-05-03T13:01:36Z")

</div>

Then something like this should work :

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

It gonna store the value in `content` field.

Cad.

---

<div class="post-metadata">

**Author:** ![marwen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marwen/32/105159_2.png) [@marwen](https://discuss.elastic.co/u/marwen)\
**Post date:** [May 4, 2022, 7:36am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/7 "2022-05-04T07:36:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 1, 2022, 7:36am UTC](https://discuss.elastic.co/t/how-to-handle-grok-optional-pattern/303796/8 "2022-06-01T07:36:42Z")

</div>

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