# Make optional character part of a defined regex (HOSTNAME)?

**URL:** <https://discuss.elastic.co/t/make-optional-character-part-of-a-defined-regex-hostname/114787>\
**Category:** Logstash\
**Created:** [January 10, 2018, 5:16am UTC](https://discuss.elastic.co/t/make-optional-character-part-of-a-defined-regex-hostname/114787 "2018-01-10T05:16:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdndude](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sdndude/32/24608_2.png) [@sdndude](https://discuss.elastic.co/u/sdndude)\
**Post date:** [January 10, 2018, 5:16am UTC](https://discuss.elastic.co/t/make-optional-character-part-of-a-defined-regex-hostname/114787/1 "2018-01-10T05:16:36Z")

</div>

I couldn't really figure out a good way to explain this in the topic heading.

I have been using the following grok filter, and up until a couple days ago, it was working fine.

```auto
 grok {
      match => {"message_id" => "%{DATA:[user_name]}:%{HOSTNAME:[domain_name]}\(%{NUMBER:[number]}\)"}
      add_field => ["received_at", "%{@timestamp}"]
      add_field => ["processed", 0]
}

```

This works great for things like the following:  
`jimmy.jones:jjones.131sku.com(187780383)`

Where it breaks is the following:  
I modified the above string to have a dot "." at the beginning of the domain\_name (it's a subtle change, that's why I am pointing it out)

`jimmy.jones:.jjones.131sku.com(187780383)`

This, of course, gets a \_grokparsefailure.

I have messed with the Grok Debugger, but I can't figure out any way to say that the dot is optional along with using the HOSTNAME. This doesn't blow up (adding the ?. before the HOSTNAME):

```auto
grok {
      match => {"message_id" => "%{DATA:[threat_name]}:?.%{HOSTNAME:[domain_name]}\(%{NUMBER:[sig_num]}\)"}
      add_field => ["received_at", "%{@timestamp}"]
      add_field => ["processed", 0]
}

```

But, it just stores the domain\_name **after** the dot, but doesn't store the dot. So, it stores the domain\_name with either string, but I am missing the dot.  
Is there a way to do this? I figure I will start messing with defining my own regex in the morning if it can't be done.

Thanks.

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [January 11, 2018, 11:16am UTC](https://discuss.elastic.co/t/make-optional-character-part-of-a-defined-regex-hostname/114787/2 "2018-01-11T11:16:35Z")

</div>

I think you should escape the dot `\.?`. In regex a dot is special and means any character.

---

<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, 2018, 11:16am UTC](https://discuss.elastic.co/t/make-optional-character-part-of-a-defined-regex-hostname/114787/3 "2018-02-08T11:16:40Z")

</div>

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