# Logstash if statement with regex example

**URL:** <https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706>\
**Category:** Logstash\
**Created:** [February 20, 2017, 10:09am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706 "2017-02-20T10:09:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [February 20, 2017, 10:09am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/1 "2017-02-20T10:09:48Z")

</div>

Can anyone show me how an if statement with a regex look like in logstash?

my statement s  
if [fieldname] =~ /^[0-9]_$/  
if [fieldname] =~ "^[0-9]_$"  
does not work

What I intend to do is to check if the "fieldname" contains an integer

---

<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:** [February 20, 2017, 10:16am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/2 "2017-02-20T10:16:13Z")

</div>

This is the correct syntax:

```
if [fieldname] =~ /^[0-9]*$/
```

---

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [February 21, 2017, 2:51am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/3 "2017-02-21T02:51:05Z")

</div>

This does not work. I think the problem is the value of the field is e.g. 1 instead of "1"

How should I handle this?

---

<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:** [February 21, 2017, 6:34am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/4 "2017-02-21T06:34:05Z")

</div>

Yes, regexp matches don't work on numbers. Perhaps you can just use normal inequality checks then? They won't be true unless the field is a number.

```
if [fieldname] >= 0 and [fieldname] < 2147483647 # or some other large number
```

---

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [February 21, 2017, 8:28am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/5 "2017-02-21T08:28:50Z")

</div>

Tried this before. Got a page of error message.

---

<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:** [February 21, 2017, 8:30am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/6 "2017-02-21T08:30:15Z")

</div>

In that case a Ruby filter might be the only way.

---

<div class="post-metadata">

**Author:** ![gringo](https://avatars.discourse-cdn.com/v4/letter/g/f19dbf/32.png) [@gringo](https://discuss.elastic.co/u/gringo)\
**Post date:** [February 21, 2017, 8:32am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/7 "2017-02-21T08:32:35Z")

</div>

did a workaround in a not so elegant way.

I created a new field and appended a blank space to it and gsub to remove the blank space. This will make it into a string and work with the regex.

---

<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:** [March 21, 2017, 8:32am UTC](https://discuss.elastic.co/t/logstash-if-statement-with-regex-example/75706/8 "2017-03-21T08:32:35Z")

</div>

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