# Painless matcher scripted field

**URL:** https://discuss.elastic.co/t/painless-matcher-scripted-field/242522
**Category:** Kibana
**Tags:** painless
**Created:** [July 24, 2020, 2:17pm UTC](https://discuss.elastic.co/t/painless-matcher-scripted-field/242522 "2020-07-24T14:17:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![marrel](https://avatars.discourse-cdn.com/v4/letter/m/a4c791/32.png) [@marrel](https://discuss.elastic.co/u/marrel)
#### Post date: [July 24, 2020, 2:17pm UTC](https://discuss.elastic.co/t/painless-matcher-scripted-field/242522/1 "2020-07-24T14:17:22Z")

</div>

hello to everyone!

I am trying to create a scripted field that will return some specific values from a field.  
This field contains a long string and I want to take some specific values which is the `some_text_here`. The values are inside`<></>` such as `<textA>some_text_here</textA>`

Please look below the content of the specific field from where I want to extract some substrings:

`....<textA>some_text_here </textA> <language>español</language> <textA>some_text_here</textA><textA>some_text_here</textA>....`

eg `....<textA>I am </textA> <language>English</language> <textA>very</textA><textA>happy</textA>....`

Please look the below code from my scripted field:

```
if (doc['content.keyword'].size()!=0) {
    def string = doc['content.keyword'].value;
    if (string != null) {
        def m = /textA(.*)textA/.matcher(string);
        if (m.find()) {
            return m.group(1);
        }
        return "no match";
    }
    return "empty"; 
}

```

With this scripted field, I can extract only the value from the first `<textA> </textA>` (eg `I am` from the example).

Have you any idea how to get also the rest `some_text_here` values simultaneously in order to have the three `some_text_here` together.  
The output value would be something like this:  
`some_text_here some_text_here some_text_here`  
eg `I am very happy` from the example.

I searched a lot here in the community but I cannot write something that working.  
Thank you in advance for any help!!!

---

<div class="post-metadata">

### Author: ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)
#### Post date: [July 30, 2020, 8:35am UTC](https://discuss.elastic.co/t/painless-matcher-scripted-field/242522/2 "2020-07-30T08:35:10Z")

</div>

Hi

didn't test it but it should work in the following way:

```
if (m.find()) {
    def result = '';
    for (int i = 1; i <=m.groupCount(); i++) {
      result+= m.group(i);
    }
    return result;
}

```

Best,  
Matthias

---

<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: [August 27, 2020, 8:35am UTC](https://discuss.elastic.co/t/painless-matcher-scripted-field/242522/3 "2020-08-27T08:35:14Z")

</div>

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