# Painless script - Can't use script params as regex pattern

**URL:** <https://discuss.elastic.co/t/painless-script-cant-use-script-params-as-regex-pattern/105045>\
**Category:** Elasticsearch\
**Created:** [October 24, 2017, 10:55am UTC](https://discuss.elastic.co/t/painless-script-cant-use-script-params-as-regex-pattern/105045 "2017-10-24T10:55:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Jacob](https://avatars.discourse-cdn.com/v4/letter/d/f17d59/32.png) [@David\_Jacob](https://discuss.elastic.co/u/David_Jacob)\
**Post date:** [October 24, 2017, 10:55am UTC](https://discuss.elastic.co/t/painless-script-cant-use-script-params-as-regex-pattern/105045/1 "2017-10-24T10:55:42Z")

</div>

Hi Team,

I am trying to use regex patterns in scripts. My problem is the regex patterns are dynamic which I'll be passing it via params for script.

In elasticsearch documentation, it is specified that only way to create pattern is using forward slashes

> /[pattern]/

I need to use param value for regex matching.

Please let me know if there is any option to specify the param type as Pattern  
or if there is any chance to convert a string variable to a Pattern obj in painless script.

Thanks,  
David Barnabas Jacob

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [October 24, 2017, 5:52pm UTC](https://discuss.elastic.co/t/painless-script-cant-use-script-params-as-regex-pattern/105045/2 "2017-10-24T17:52:50Z")

</div>

This is not possible. For performance reasons, regexes must be known at compile time. You could generate your script source based on the regex, however I would be wary of this because if you have a lot of patterns, it will cause thrashing within the script cache (which has limits in place to protect from that exact scenario, you may hit "max compliations per minute).

---

<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:** [November 21, 2017, 5:52pm UTC](https://discuss.elastic.co/t/painless-script-cant-use-script-params-as-regex-pattern/105045/3 "2017-11-21T17:52:53Z")

</div>

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