# Painless: match a string

**URL:** <https://discuss.elastic.co/t/painless-match-a-string/143768>\
**Category:** Elasticsearch\
**Created:** [August 9, 2018, 9:16pm UTC](https://discuss.elastic.co/t/painless-match-a-string/143768 "2018-08-09T21:16:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![palace](https://avatars.discourse-cdn.com/v4/letter/p/e47774/32.png) [@palace](https://discuss.elastic.co/u/palace)\
**Post date:** [August 9, 2018, 9:16pm UTC](https://discuss.elastic.co/t/painless-match-a-string/143768/1 "2018-08-09T21:16:43Z")

</div>

Hello,

I am new to painless and wanted to know what is the proper way to match a string.

For my specific example I want to perform two different manipulations to two keywords and then combine them at the end. The first manipulation involves taking only the string before the '/', which I have done. The second manipulation involves finding a particular word in a long string. For example, finding the word "cat" or "dog" in the phrase doc['sentence.keyword'] = "I\_am\_a\_(dog or cat)\_pet". If the phrase found is cat then add that to the first manipulation so that the final result becomes: "This\_is\_a\_cat", if the phrase found is dog then it becomes "This\_is\_a\_dog". There are no other combinations than cat or dog.

Looking at the Painless scripting blog:

> **[Using Painless in Kibana scripted fields
	  	 | Elastic](https://www.elastic.co/blog/using-painless-kibana-scripted-fields)**
>
> Kibana provides powerful ways to search and visualize data stored in Elasticsearch. For the purpose of visualizations, Kibana looks for fields defined in Elasticsearch mappings and presents them as op...

I see that there are two examples, one which matches a string using regex and another which matches a string using .matcher.

For my problem, what is the best way to match the keyword, either cat or dog, and then add that to the first manipulation. Please include example code.

Thank you in advance,

---

<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:** [September 6, 2018, 9:22pm UTC](https://discuss.elastic.co/t/painless-match-a-string/143768/2 "2018-09-06T21:22:10Z")

</div>

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