# How to use array variable in regex. Painless scripted field

**URL:** <https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527>\
**Category:** Kibana\
**Created:** [January 22, 2018, 7:27pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527 "2018-01-22T19:27:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)\
**Post date:** [January 22, 2018, 7:27pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/1 "2018-01-22T19:27:25Z")

</div>

Hi

How can I use an array value in a regex? Such as:

String returnValue = "";  
String[] listarray;  
listarray = new String[2];  
listarray[0] = "Movies";  
listarray[1] = "PG13";

for( int i = 0; i \< listarray.size(); i ++ )  
{  
if (count =~ /${listarray[i]}/) {  
returnValue += listarray;  
returnValue += " ";  
}

Many thanks!

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [January 22, 2018, 9:22pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/2 "2018-01-22T21:22:02Z")

</div>

Hello,

What are you trying to accomplish exactly in that script? There might be a better/easier way to do it.  
But from what I can see, this line:  
`if (count =~ /${listarray[i]}/)` should be changed to `if (count =~ /listarray[i]/)`  
and this line:  
`returnValue += listarray;` should probably be `returnValue += listarray[i];`

---

<div class="post-metadata">

**Author:** ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)\
**Post date:** [January 22, 2018, 9:54pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/3 "2018-01-22T21:54:26Z")

</div>

> [@Marius\_Dragomir](#):
>
> and this line:

Thanks!

I found an easier way to do it. Do you happen to know how I can use curl to save scripted fields?

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [January 22, 2018, 9:59pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/4 "2018-01-22T21:59:13Z")

</div>

Yes, this is how to add one to Kibana.

> [@Scripted field with curl](https://discuss.elastic.co/t/scripted-field-with-curl/106837):
>
> Is it possible to add a scripted filed to an index with curl or in the Kibana console? If yes please post a link or give an example.

saving one is just doing the same and looking for the field name inside the JSON you get from cURL.

---

<div class="post-metadata">

**Author:** ![dr\_dob](https://avatars.discourse-cdn.com/v4/letter/d/71e660/32.png) [@dr\_dob](https://discuss.elastic.co/u/dr_dob)\
**Post date:** [January 23, 2018, 7:51pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/5 "2018-01-23T19:51:53Z")

</div>

Thanks for the response.  
Read the discussion. Since the index-pattern field value is a string and not an array, I cant update it with "source": "ctx.\_source.tags.add(params.tag)" by adding to the array. So what is the best way to update the field?

Is there a way to simply replace it with something like source": "ctx.\_source.fields = params.field"

---

<div class="post-metadata">

**Author:** ![Marius\_Dragomir](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marius_dragomir/32/42087_2.png) [@Marius\_Dragomir](https://discuss.elastic.co/u/Marius_Dragomir)\
**Post date:** [January 25, 2018, 3:33pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/6 "2018-01-25T15:33:56Z")

</div>

Wait, are you trying to create more scripted fields from inside a scripted field?

---

<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:** [February 22, 2018, 3:34pm UTC](https://discuss.elastic.co/t/how-to-use-array-variable-in-regex-painless-scripted-field/116527/7 "2018-02-22T15:34:01Z")

</div>

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