# How to use regex in kibana painless script?

**URL:** https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174
**Category:** Kibana
**Created:** [January 14, 2019, 4:28pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174 "2019-01-14T16:28:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mouli632](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mouli632](https://discuss.elastic.co/u/mouli632)
#### Post date: [January 14, 2019, 4:28pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/1 "2019-01-14T16:28:12Z")

</div>

given query is working in kibana query DSL  
{  
"query": {  
"regexp": {  
"path": "/hma/v1.0/.\*"  
}  
}  
}  
can you please help me How to write painless script for above query?

---

<div class="post-metadata">

### Author: ![stiltz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stiltz/32/39714_2.png) [@stiltz](https://discuss.elastic.co/u/stiltz)
#### Post date: [January 14, 2019, 5:02pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/2 "2019-01-14T17:02:27Z")

</div>

What additional value are you hoping to get from a painless script vs. your query? Without knowing that then I'd stick with your query as is.

Here are painless examples: [https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-examples.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-examples.html)  
RegEx is currently disabled by default for painless scripting.

---

<div class="post-metadata">

### Author: ![mouli632](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mouli632](https://discuss.elastic.co/u/mouli632)
#### Post date: [January 14, 2019, 5:40pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/4 "2019-01-14T17:40:33Z")

</div>

Thanks for quick reply stilts.  
I want to use same query to create a new field by using kibana painless script however I am not able to do that.  
My painless script is:  
if(doc["path"].value == "/hma/v1.0/customer/.\*")  
{  
return customer  
}

== is working for only exactly matched records not for regex.  
What operator should I use for regex?  
I have enabled regex in my elasticsearch.yml file.

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 14, 2019, 5:54pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/5 "2019-01-14T17:54:37Z")

</div>

I am not sure I understand why you are trying to do what you do. A bit of background to the problem you are trying to solve would be useful.

Using scripted fields with regular expressions is likely to be quite slow and not scale well. If this data is something you want to do analysis on on a regular basis the best way is to extract it and put it into a separate field before you index the data into Elasticsearch.

---

<div class="post-metadata">

### Author: ![mouli632](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mouli632](https://discuss.elastic.co/u/mouli632)
#### Post date: [January 14, 2019, 6:20pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/6 "2019-01-14T18:20:19Z")

</div>

Hi Christian thanks for your quick response.  
I don't understand clearly can you please explain with query.

"the best way is to extract it and put it into a separate field before you index the data into Elasticsearch."

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [January 14, 2019, 6:28pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/7 "2019-01-14T18:28:01Z")

</div>

Which parts of the string is it you want to filter/search/aggregate on? What is it you want to do with this field/data? What does the different parts of the path represent?

---

<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 11, 2019, 6:28pm UTC](https://discuss.elastic.co/t/how-to-use-regex-in-kibana-painless-script/164174/8 "2019-02-11T18:28:15Z")

</div>

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