# Logstash: If message contains regex add field

**URL:** <https://discuss.elastic.co/t/logstash-if-message-contains-regex-add-field/86799>\
**Category:** Logstash\
**Created:** [May 23, 2017, 11:36am UTC](https://discuss.elastic.co/t/logstash-if-message-contains-regex-add-field/86799 "2017-05-23T11:36:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasdf](https://avatars.discourse-cdn.com/v4/letter/j/50afbb/32.png) [@jasdf](https://discuss.elastic.co/u/jasdf)\
**Post date:** [May 23, 2017, 11:36am UTC](https://discuss.elastic.co/t/logstash-if-message-contains-regex-add-field/86799/1 "2017-05-23T11:36:57Z")

</div>

Hello,

within my Logstash config I'm searching for a regex pattern. If a logfile is found which matches the regex a new field should be added with the found pattern.  
The if statement is working but I don't know how to save the exact pattern to a new field. This is my code at the moment:

```
input {
  stdin { }
}

filter {
  if [message] =~ "[0-9|A-Z]{11}" {
    mutate {
      add_field => {"message-id" => "found pattern from above" }
    }
  }

output {
  stdout { codec => rubydebug }
}
```

---

<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:** [June 20, 2017, 11:37am UTC](https://discuss.elastic.co/t/logstash-if-message-contains-regex-add-field/86799/2 "2017-06-20T11:37:10Z")

</div>

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