# Match multiple 4 digit numbers in field

**URL:** <https://discuss.elastic.co/t/match-multiple-4-digit-numbers-in-field/264494>\
**Category:** Logstash\
**Created:** [February 16, 2021, 11:35pm UTC](https://discuss.elastic.co/t/match-multiple-4-digit-numbers-in-field/264494 "2021-02-16T23:35:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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:** [February 17, 2021, 12:28am UTC](https://discuss.elastic.co/t/match-multiple-4-digit-numbers-in-field/264494/2 "2021-02-17T00:28:35Z")

</div>

grok will not match the same pattern repeatedly. I use the .scan function of a ruby string to do this You could try

```
    ruby {
        code => '
            created = event.get("created")
            if created
                matches = created.scan(/\d{4}/)
                # matches will be an array like ["2010"] or ["2000", "1994"]
                # Convert the array to integers
                matches = matches.map(&:to_i)
                event.set("mostRecentYear", matches.max)
            end
        '
    }

```

That assumes your field name is [created], not [created:]. You can overwrite [created] instead of adding a new field by changing the event.set line.

If you want to restrict the matches to strictly four digit numbers, and not match part of 123456 change the regexp to /\D\d{4}\D/

---

_[View the full topic](https://discuss.elastic.co/t/match-multiple-4-digit-numbers-in-field/264494)._
