# Logstash: regex to get part of file name

**URL:** https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673
**Category:** Logstash
**Created:** [December 15, 2020, 5:22am UTC](https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673 "2020-12-15T05:22:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nameisnotimportant](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@nameisnotimportant](https://discuss.elastic.co/u/nameisnotimportant)
#### Post date: [December 15, 2020, 5:22am UTC](https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673/1 "2020-12-15T05:22:46Z")

</div>

Hi,

I am trying to get the date in the file name using following regex, but it still return a-zA-Z character:

Path:  
/home/appadmin/Documents/ML/data/pocket\_pmo/activityLogs20201211.csv

> grok { match =\> { "path" =\> "(?\<[@metadata][indexName]\>[^/][\d]\*)....$" } }  
> mutate {  
> add\_field =\> {"indexname" =\> "%{[@metadata][indexName]}" }  
> }

result:

> "indexname" =\> "s20201211"

May i know why the character 's' still there? How do i get only the date ?

Regards.

---

<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: [December 15, 2020, 3:10pm UTC](https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673/2 "2020-12-15T15:10:58Z")

</div>

> [@nameisnotimportant](#):
>
> May i know why the character 's' still there?

`"(?<[@metadata][indexName]>[^/][\d]*)` captures a single character that is not slash (i.e. the 's'), followed by zero or more digits. You already have the pattern anchored to the end of the string, and I see no reason to include the `[^/]`.

---

<div class="post-metadata">

### Author: ![nameisnotimportant](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@nameisnotimportant](https://discuss.elastic.co/u/nameisnotimportant)
#### Post date: [December 16, 2020, 12:39am UTC](https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673/3 "2020-12-16T00:39:52Z")

</div>

If i remove [^/] to become the following:

> grok { match =\> { "path" =\> "(?\<[@metadata][indexName]\>[\d]\*)....$" } }

i got the following results:

> "indexname" =\> "%{[@metadata][indexName]}"

---

<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: [January 13, 2021, 12:39am UTC](https://discuss.elastic.co/t/logstash-regex-to-get-part-of-file-name/258673/4 "2021-01-13T00:39:53Z")

</div>

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