# Regex field starts with a string

**URL:** <https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235>\
**Category:** Logstash\
**Created:** [September 10, 2020, 8:23pm UTC](https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235 "2020-09-10T20:23:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ankitachow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitachow/32/46246_2.png) [@ankitachow](https://discuss.elastic.co/u/ankitachow)\
**Post date:** [September 10, 2020, 8:23pm UTC](https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235/1 "2020-09-10T20:23:35Z")

</div>

I want to add a new field if a field start with a certain string. Added below code in filter { }, to set a new field error as "Y" if text starts with ERROR: . Doesnt seem to work.

```
  if "[dest][text]" =~ /^ERROR: .*/ {
     mutate { add_field => { "error" => "Y" } }
  }
```

---

<div class="post-metadata">

**Author:** ![Dinh\_Dinh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dinh_dinh/32/73861_2.png) [@Dinh\_Dinh](https://discuss.elastic.co/u/Dinh_Dinh)\
**Post date:** [September 10, 2020, 10:10pm UTC](https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235/2 "2020-09-10T22:10:47Z")

</div>

Can you try the following

```auto
     if [dest][text] =~ "ERROR:" {
     mutate { add_field => { "error" => "Y" } }
  }

```

---

<div class="post-metadata">

**Author:** ![ankitachow](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ankitachow/32/46246_2.png) [@ankitachow](https://discuss.elastic.co/u/ankitachow)\
**Post date:** [September 11, 2020, 1:12pm UTC](https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235/3 "2020-09-11T13:12:29Z")

</div>

This I have tried already but the above code will check for ERROR: any where in the text field and not specifically starting with ERROR:

Small syntax change that worked: (to check for ERROR: anywhere in the text)

```
  if ([dest][text] =~ "ERROR:") {
     mutate { add_field => { "error" => "ERROR" } }

```

The above syntax change in my previous code worked to see if text begins with ERROR:

```
if ([dest][text] =~ /^ERROR:.*/ ) {
      mutate { add_field => { "error" => "Y" } }
}
```

---

<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:** [October 9, 2020, 1:12pm UTC](https://discuss.elastic.co/t/regex-field-starts-with-a-string/248235/4 "2020-10-09T13:12:35Z")

</div>

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