# Regex pattern\_replace

**URL:** <https://discuss.elastic.co/t/regex-pattern-replace/214047>\
**Category:** Elasticsearch\
**Created:** [January 7, 2020, 12:22pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047 "2020-01-07T12:22:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elink12](https://avatars.discourse-cdn.com/v4/letter/e/5daacb/32.png) [@elink12](https://discuss.elastic.co/u/elink12)\
**Post date:** [January 7, 2020, 12:22pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/1 "2020-01-07T12:22:03Z")

</div>

Hi friend, I have a problem. I don’t understand why it works like this?

#My filter pattern  
'my\_pattern\_replace' =\> [  
"type" =\> "pattern\_replace",  
"pattern" =\> "([0-9.,-]+)\s?(car\b|cars\b|cars\w+)",  
"replacement" =\> "$1car"

```
                                    ],

```

#test against :

i have 2 cars and 2cars

#String replacement result analyze:

 ![Clip2net_200107152101](https://us1.discourse-cdn.com/elastic/original/3X/f/4/f463a9b8e5909aa81267b65b4972eb231205f266.png)

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [January 7, 2020, 4:44pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/2 "2020-01-07T16:44:31Z")

</div>

Hello and welcome to the forums!

Your example uses a `pattern_replace`, which is a token filter. It operates on individual tokens in the text, rather than on the string as a whole. By the time the string `i have 2 cars and 2cars` gets to the filter, it's been transformed into a stream of tokens: `['i', 'have', '2', 'cars', 'and', '2cars']`. The regex gets applied to each of those tokens, and `2` and `cars` don't match on their own, but the `2cars` token does.

If you wanted `2 cars` to be its own token, that's something that would be controlled by a "tokenizer" rather than a "token filter." For more details, see [anatomy of an analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analyzer-anatomy.html).

-William

---

<div class="post-metadata">

**Author:** ![elink12](https://avatars.discourse-cdn.com/v4/letter/e/5daacb/32.png) [@elink12](https://discuss.elastic.co/u/elink12)\
**Post date:** [January 7, 2020, 6:03pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/4 "2020-01-07T18:03:11Z")

</div>

I found this [https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-pattern-replace-charfilter.html)

it's work

---

<div class="post-metadata">

**Author:** ![elink12](https://avatars.discourse-cdn.com/v4/letter/e/5daacb/32.png) [@elink12](https://discuss.elastic.co/u/elink12)\
**Post date:** [January 7, 2020, 6:13pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/5 "2020-01-07T18:13:16Z")

</div>

but I had another problem, the snowball is used after char\_filter so my regular expression does not always work. Are there any solutions?

---

<div class="post-metadata">

**Author:** ![William\_Brafford](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/william_brafford/32/51559_2.png) [@William\_Brafford](https://discuss.elastic.co/u/William_Brafford)\
**Post date:** [January 8, 2020, 3:18pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/6 "2020-01-08T15:18:56Z")

</div>

There is a lot that you can do with custom analyzers, but I'm not sure if I understand your use case fully. What is the problem you are trying to solve with this regular expression?

-William

---

<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 5, 2020, 3:18pm UTC](https://discuss.elastic.co/t/regex-pattern-replace/214047/7 "2020-02-05T15:18:57Z")

</div>

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