# Logstash if condition regex

**URL:** <https://discuss.elastic.co/t/logstash-if-condition-regex/223141>\
**Category:** Logstash\
**Created:** [March 11, 2020, 1:23pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141 "2020-03-11T13:23:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ash2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ash2/32/45433_2.png) [@ash2](https://discuss.elastic.co/u/ash2)\
**Post date:** [March 11, 2020, 1:23pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/1 "2020-03-11T13:23:05Z")

</div>

I want to write an if condition which takes regex for file path of window directory in logstash. Please help me with the regex pattern of` C:\Windows\System32\logs\*.log` .  
e.g.  
output{  
if [log][file][path] =~ /`C:\Windows\System32\logs\*.log`/ {  
pipeline {send\_to =\> somepipeline }  
}  
}

---

<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:** [March 11, 2020, 3:04pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/2 "2020-03-11T15:04:07Z")

</div>

You really need to use \</\> blockquote markup around configurations, because otherwise some characters from the regexp will get consumed as markup. Edit your post, select the configuration, then click on \</\> in the toolbar above the edit pane. You will see the appearance change in the preview pane on the right from

_if [log][file][path] =~ /C:\Windows\System32\logs._ .log/ {

to

```
if [log][file][path] =~ /C:\Windows\System32\logs..log/ {

```

---

<div class="post-metadata">

**Author:** ![rcowart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rcowart/32/88091_2.png) [@rcowart](https://discuss.elastic.co/u/rcowart)\
**Post date:** [March 11, 2020, 4:33pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/3 "2020-03-11T16:33:48Z")

</div>

You will need to escape those backslashes...

```auto
if [log][file][path] =~ /C:\\Windows\\System32\\logs\\*.log/ {

```

`.` means any character in regex, so you will need to escape that as well.

```auto
if [log][file][path] =~ /C:\\Windows\\System32\\logs\\*\.log/ {

```

`*` means none or more of the previous character (it is not a simple greedy wildcard). `+` means one or more of the following character. Combined with `.` these mean:

`.*` - none or more of any character  
`.+` - one or more of any character

You probably want the latter, which will give you...

```auto
if [log][file][path] =~ /C:\\Windows\\System32\\logs\\.+\.log/ {

```

You can test this at [https://regex101.com/](https://regex101.com/)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/b/2ba14d1fd41822bc4b2fe8f104c2d889d506656b.png)

---

<div class="post-metadata">

**Author:** ![ash2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ash2/32/45433_2.png) [@ash2](https://discuss.elastic.co/u/ash2)\
**Post date:** [March 11, 2020, 7:22pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/4 "2020-03-11T19:22:26Z")

</div>

Will this regex work in logstash configuration file?

---

<div class="post-metadata">

**Author:** ![rcowart](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rcowart/32/88091_2.png) [@rcowart](https://discuss.elastic.co/u/rcowart)\
**Post date:** [March 11, 2020, 8:00pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/5 "2020-03-11T20:00:29Z")

</div>

Yes... as I corrected your line in my reply.

---

<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:** [April 8, 2020, 8:11pm UTC](https://discuss.elastic.co/t/logstash-if-condition-regex/223141/6 "2020-04-08T20:11:27Z")

</div>

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