# Grok capture index value with different patterns within the message

**URL:** <https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770>\
**Category:** Logstash\
**Created:** [June 20, 2019, 9:27pm UTC](https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770 "2019-06-20T21:27:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_lim/32/45922_2.png) [@John\_Lim](https://discuss.elastic.co/u/John_Lim)\
**Post date:** [June 20, 2019, 9:27pm UTC](https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770/1 "2019-06-20T21:27:55Z")

</div>

Thank you in advance,

```
 Please advise how can I capture logs that have different pattern, let say for username, the value shows [ldap:my_user01], and I created grok to capture this by \[ldap0:%{USERNAME:username}\], then later on, I found out some logs wth non ldap username, the value shows [ANON], how can I extract just the username (without ldap:).....
```

---

<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:** [June 20, 2019, 9:45pm UTC](https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770/2 "2019-06-20T21:45:38Z")

</div>

You can ? to make a capture group optional.

```
grok { match => { "field" => "^\[(%{WORD:ldap}:)?%{USER:user}\]$" } }
```

---

<div class="post-metadata">

**Author:** ![John\_Lim](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/john_lim/32/45922_2.png) [@John\_Lim](https://discuss.elastic.co/u/John_Lim)\
**Post date:** [June 25, 2019, 1:57pm UTC](https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770/3 "2019-06-25T13:57:16Z")

</div>

Thank you so much..appreciate it..

---

<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 23, 2019, 1:57pm UTC](https://discuss.elastic.co/t/grok-capture-index-value-with-different-patterns-within-the-message/186770/4 "2019-07-23T13:57:25Z")

</div>

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