# Remove leading and trailing punctuation from terms

**URL:** <https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063>\
**Category:** Elasticsearch\
**Created:** [March 6, 2019, 9:37am UTC](https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063 "2019-03-06T09:37:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bram](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@bram](https://discuss.elastic.co/u/bram)\
**Post date:** [March 6, 2019, 9:37am UTC](https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063/1 "2019-03-06T09:37:46Z")

</div>

How can I **only** remove the leading and trailing punctuation from terms when indexing?

The standard tokenizer also splits words like "t-shirt" into "t" and "shirt", and that is not desired in our use case. But we want to turn "t-shirt." into "t-shirt".

Is there a filter available that does this?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 7, 2019, 10:30am UTC](https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063/2 "2019-03-07T10:30:27Z")

</div>

You could use a character filter that changes a hyphen in the middle of a word into an underscore. Underscores are not removed by the standard tokenizer. Something like this:

```auto
GET _analyze
{
  "text": [
    "t-shirt t-shirt."
  ],
  "tokenizer": "standard",
  "char_filter": [
    {
      "type": "pattern_replace",
      "pattern": "(\\w+)-(?=\\w)",
      "replacement": "$1_"
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![bram](https://avatars.discourse-cdn.com/v4/letter/b/9d8465/32.png) [@bram](https://discuss.elastic.co/u/bram)\
**Post date:** [March 7, 2019, 11:48am UTC](https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063/3 "2019-03-07T11:48:31Z")

</div>

Thanks 😄, that has fixed our problems; together with a patternreplace as the first term filter to make sure the worddelimiter keeps working (for the terms "t-shirt", "t", "shirt" and "tshirt").

---

<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:** [April 4, 2019, 11:50am UTC](https://discuss.elastic.co/t/remove-leading-and-trailing-punctuation-from-terms/171063/4 "2019-04-04T11:50:17Z")

</div>

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