# Replacing special characters with gsub

**URL:** <https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098>\
**Category:** Logstash\
**Created:** [April 1, 2020, 5:30pm UTC](https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098 "2020-04-01T17:30:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)\
**Post date:** [April 1, 2020, 5:30pm UTC](https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098/1 "2020-04-01T17:30:36Z")

</div>

I'm using gsub and regex to structure a field into json. Unfortunately, I'm having problems with double-quotes being escaped in the output. For example:

Field: event  
Value: 'success': True

```auto
"event", "'", "\"",

```

Expected output `"success": True`

Actual output:  
Kibana Table format  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/6/563f59eadd58de299a3eecc3782a892f8e0e6f14.png)

Kibana JSON format  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/0/20717964cea07c2e62782b9d88505899cca1d7a3.png)

Logstash pipeline config:  
 ![image](https://us1.discourse-cdn.com/elastic/original/3X/3/e/3e04370042a1849302b7f3b2a5f7e4ea0422c226.png)

If I change the pipeline config and omit the escaping backslash character, Logstash throws an error: `Expected one of [\\t\\r\\n], \"#\", \"{\", \",\", \"]\" at line 215, column 29 `

Am I escaping special characters wrong with gsub?

---

<div class="post-metadata">

**Author:** ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)\
**Post date:** [April 1, 2020, 5:34pm UTC](https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098/2 "2020-04-01T17:34:17Z")

</div>

Looking further into what im doing, it looks like gsub also doesn't support using capture groups and substitution? For example `"event", "string (capture this)", "$1"`

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [April 1, 2020, 6:17pm UTC](https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098/3 "2020-04-01T18:17:32Z")

</div>

Capture groups are supported. Use "\1" to reference the first group.

If you need to use double quotes in a string then surround it with single quotes

```
mutate { gsub => ["event", "'", '"'] }
```

---

<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 29, 2020, 6:17pm UTC](https://discuss.elastic.co/t/replacing-special-characters-with-gsub/226098/4 "2020-04-29T18:17:49Z")

</div>

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