# Pattern\_replace char filter regex

**URL:** https://discuss.elastic.co/t/pattern-replace-char-filter-regex/87798
**Category:** Elasticsearch
**Created:** [May 31, 2017, 7:42pm UTC](https://discuss.elastic.co/t/pattern-replace-char-filter-regex/87798 "2017-05-31T19:42:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![saurav](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/saurav/32/26497_2.png) [@saurav](https://discuss.elastic.co/u/saurav)
#### Post date: [May 31, 2017, 7:42pm UTC](https://discuss.elastic.co/t/pattern-replace-char-filter-regex/87798/1 "2017-05-31T19:42:14Z")

</div>

This is my index settings: PUT my\_index  
{  
"settings": {  
"analysis": {  
"analyzer": {  
"my\_analyzer": {  
"tokenizer": "standard",  
"char\_filter": [  
"my\_char\_filter"  
]  
}  
},  
"char\_filter": {  
"my\_char\_filter": {  
"type": "pattern\_replace",  
"pattern": "^([a-zA-Z0-9\_]+)-([a-zA-Z0-9\_]+)",  
"replacement": "$1$2"  
}  
}  
}  
}  
}

I'm trying to analyze this query:  
POST my\_index/\_analyze  
{"analyzer":"my\_analyzer","text":"elastic-search"}  
POST my\_index/\_analyze  
{"analyzer":"my\_analyzer","text":"-search"}

Case 1 works fine but for case 2, i get the token 'search' which is not what i want. I want it to skip it if i don't provide text preceding the hyphen. What am i doing wrong?

---

<div class="post-metadata">

### Author: ![Mike.Barretta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mike.barretta/32/16688_2.png) [@Mike.Barretta](https://discuss.elastic.co/u/Mike.Barretta)
#### Post date: [May 31, 2017, 9:54pm UTC](https://discuss.elastic.co/t/pattern-replace-char-filter-regex/87798/2 "2017-05-31T21:54:46Z")

</div>

I think the issue is the use of the standard tokenizer, which removes the hyphen before the char\_filter gets the chance.

Instead, you could use something like the whitespace tokenizer:

```
PUT test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "whitespace",
          "char_filter": [
            "my_char_filter"
          ]
        }
      } ,
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern":"(\\w+)-(\\w+)",
          "replacement": "$1$2"
        }
      }
    }
  }
}
```

---

<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: [June 28, 2017, 9:55pm UTC](https://discuss.elastic.co/t/pattern-replace-char-filter-regex/87798/3 "2017-06-28T21:55:07Z")

</div>

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