# Optional match for a grok pattern

**URL:** <https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904>\
**Category:** Logstash\
**Created:** [January 11, 2023, 10:24am UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904 "2023-01-11T10:24:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![moep](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@moep](https://discuss.elastic.co/u/moep)\
**Post date:** [January 11, 2023, 10:24am UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904/1 "2023-01-11T10:24:59Z")

</div>

Hey community,

I would like to build an optional match for the following logline:

```auto
2023-01-11 00:00:11 1pEoP9-000LLu-Gz <= noreply@domain.de H=fqdn.domain.de (FQDN) [10.1.1.1] P=esmtpa A=login_virtual_exim:mtaspooler@domain.de S=19312 

```

I'm using the following grok pattern to match, but it doesnt match at the beginn of A= in the logline.

```auto
(%{EXIM_DATE}) (%{EXIM_MSGID}) (%{EXIM_FLAGS}) ?(%{EMAILADDRESS:exim_sender}) ?(%{EXIM_REMOTE_HOST}) ?(%{EXIM_PROTOCOL}) ?(X=(?<exim_tls>TLS1.[0-9]):(%{NOTSPACE:exim_ciphers})) ?(CV=%{WORD:exim_cv_value}) ?(A=%{NOTSPACE:exim_mtaspooler}) ?(%{EXIM_MSG_SIZE}) ?(%{EXIM_HEADER_ID})

```

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [January 11, 2023, 2:58pm UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904/2 "2023-01-11T14:58:39Z")

</div>

> [@moep](#):
>
> I'm using the following grok pattern to match, but it doesnt match at the beginn of A= in the logline.

That is because the CV= field is required, not optional. I suggest making all those fields optional (assuming that they are) and pulling the ` ?` inside the sub-patterns.

```
 grok { match => { "message" => "(%{EXIM_DATE}) (%{EXIM_MSGID}) (%{EXIM_FLAGS}) ?(%{EMAILADDRESS:exim_sender}) ?(%{EXIM_REMOTE_HOST}) ?(%{EXIM_PROTOCOL})( X=(?<exim_tls>TLS1.[0-9]):(%{NOTSPACE:exim_ciphers}))?( CV=%{WORD:exim_cv_value})?( A=%{NOTSPACE:exim_mtaspooler})?( %{EXIM_MSG_SIZE})?( %{EXIM_HEADER_ID})?" } }

```

---

<div class="post-metadata">

**Author:** ![moep](https://avatars.discourse-cdn.com/v4/letter/m/ad7895/32.png) [@moep](https://discuss.elastic.co/u/moep)\
**Post date:** [January 11, 2023, 3:02pm UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904/3 "2023-01-11T15:02:54Z")

</div>

so basicly you removed the `?` outside, so its optional changed the whitespace in the brackets?

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [January 11, 2023, 3:05pm UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904/4 "2023-01-11T15:05:46Z")

</div>

I changed

```
 ?(CV=%{WORD:exim_cv_value})

```

to

```
( CV=%{WORD:exim_cv_value})?

```

and likewise for the other fields.

---

<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:** [February 8, 2023, 3:06pm UTC](https://discuss.elastic.co/t/optional-match-for-a-grok-pattern/322904/5 "2023-02-08T15:06:35Z")

</div>

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