# How to capture regex groups with /g?

**URL:** https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858
**Category:** Logstash
**Created:** [July 19, 2016, 10:35am UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858 "2016-07-19T10:35:55Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)
#### Post date: [July 19, 2016, 10:35am UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858/1 "2016-07-19T10:35:55Z")

</div>

Hi,

I have following excerpt of a log line:

... [NO SORT], #0...#4000 (PageSize: 4001), , Fetched: 13, 687 ms0...END (PageSize: INFINITE), , Fetched: 147, 15 ms0...END (PageSize: INFINITE), , Fetched: 147, 16 ms0...END (PageSize: INFINITE), , Fetched: 147, 16 ms0...END (PageSize: INFINITE), , Fetched: 147, 15 ms0...END (PageSize: INFINITE), , Fetched: 147, 31 ms0...END (PageSize: INFINITE), , Fetched: 147, 47 ms0...END (PageSize: INFINITE), , Fetched: 147, 16 ms0...END (PageSize: INFINITE), , Fetched: 147, 15 ms0...END (PageSize: INFINITE), , Fetched: 147, 0 ms0...END (PageSize: INFINITE), , Fetched: 147, 32 ms0...END (PageSize: INFINITE), , Fetched: 147, 46 ms0...END (PageSize: INFINITE), , Fetched: 147, 16 ms0...END (PageSize: INFINITE), , Fetched: 147, 16 ms

I need to extract all given ms and aggregate them together and save them as field.  
In regex I can get the values as following:

```auto

```

This gives me 14 regex groups back.

But how do I do it in logstash? And how do I aggregate them together? (value1+value2+value3...)

Thanks, Andreas

---

<div class="post-metadata">

### Author: ![PhaedrusTheGreek](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/phaedrusthegreek/32/4884_2.png) [@PhaedrusTheGreek](https://discuss.elastic.co/u/PhaedrusTheGreek)
#### Post date: [July 19, 2016, 4:31pm UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858/2 "2016-07-19T16:31:17Z")

</div>

OK @asp,

Would you share the ruby filter here when you have it working?

---

<div class="post-metadata">

### Author: ![asp](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@asp](https://discuss.elastic.co/u/asp)
#### Post date: [July 20, 2016, 11:08am UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858/3 "2016-07-20T11:08:05Z")

</div>

I am currently heading for workaround it with the ruby filter.

I have written a ruby script which does the regex search and aggregates the data., but its not finished yet

---

<div class="post-metadata">

### Author: ![aya](https://avatars.discourse-cdn.com/v4/letter/a/a6a055/32.png) [@aya](https://discuss.elastic.co/u/aya)
#### Post date: [September 8, 2016, 10:46am UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858/4 "2016-09-08T10:46:53Z")

</div>

I had a similar need and had to use the ruby filter too.  
This example capture each block of alpha characters from the field 'input' and add them to a new field 'groups'.

```
filter {
    ruby {
      code => "event['groups'] = event['input'].downcase.scan(/[[:alpha:]]+/)"
    }
}

```

input = "Module1-Customer-Region\_2-FuncX-Label42"  
groups = ["module", "customer", "region", "funcx", "label"]

---

<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: [July 6, 2017, 4:39am UTC](https://discuss.elastic.co/t/how-to-capture-regex-groups-with-g/55858/5 "2017-07-06T04:39:17Z")

</div>


