# Making a part in the grok expression optional

**URL:** https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961
**Category:** Logstash
**Created:** [March 9, 2016, 11:29pm UTC](https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961 "2016-03-09T23:29:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![sankallada](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@sankallada](https://discuss.elastic.co/u/sankallada)
#### Post date: [March 9, 2016, 11:29pm UTC](https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961/1 "2016-03-09T23:29:17Z")

</div>

Hi all,

Each line in my log file does not contain a source IP adderss so the

```
   "message" => "%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:loglevel}\s+%{THREAD:thread}\s+(?:%{IP:ip})" 

```

so it returns a "no mach" - from grok debugger. As result the the fields that I'm expecting to be created by the grok{} is not happening. So we tried to make the pattern for IP in this expression something like :

```
  match => {"message" => "%{TIMESTAMP_ISO8601:timestamp}\s+%{LOGLEVEL:loglevel}\s+%{THREAD:thread}\s+(?:%{IP:ip})"}

```

Correction: It seems I had made a mistake. This new expression also not delivering the expected result. i.e If IP is not present then the expression returns a no match. Is there a way to solve this?

**NB** : _Haven't yet tested the same in logstash and kibana for field creation still working with grok debugger where it shows a success. Also THREAD:thread is a custom pattern. Like THREAD [\s\*\b(?:[0-9A-Za-z][0-9A-Za-z_-]{0,62})(?:.(?:[0-9_A-Za-z][0-9A-Za-z-]{0,62})‌​)_(.?|\b)\s\*] \_

Continuing the discussion from [Grok for parsing java Log](https://discuss.elastic.co/t/grok-for-parsing-java-log/43718/7):

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [March 10, 2016, 7:10am UTC](https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961/2 "2016-03-10T07:10:14Z")

</div>

Use the ? operator to denote "zero or one occurrence of the previous token", so e.g. `(?:%{IP:ip})?` (or maybe `%{IP:ip}?` is enough) although you probably want `(?:\s+%{IP:ip})` so that the spaces are optional too.

---

<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: [July 6, 2017, 5:07am UTC](https://discuss.elastic.co/t/making-a-part-in-the-grok-expression-optional/43961/3 "2017-07-06T05:07:37Z")

</div>


