# Replace middle of string

**URL:** <https://discuss.elastic.co/t/replace-middle-of-string/96382>\
**Category:** Logstash\
**Created:** [August 9, 2017, 6:46am UTC](https://discuss.elastic.co/t/replace-middle-of-string/96382 "2017-08-09T06:46:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![VincentNgai86](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@VincentNgai86](https://discuss.elastic.co/u/VincentNgai86)\
**Post date:** [August 9, 2017, 6:46am UTC](https://discuss.elastic.co/t/replace-middle-of-string/96382/1 "2017-08-09T06:46:26Z")

</div>

Hi All ,

Have question about how to gsub or replace some of middle string

The orginal body like this  
A=somestinghere1&B=somestringhere2&C=somestringhere3&D=somestringhere4

What i would like to do like remain A,C and D  
A=somestinghere1&B= **Replace\_ByELK** &C=somestringhere3&D=somestringhere4

I try below but no luck , i will erase all wording after B=Replace\_ByELK until the end of &

A=somestinghere1&B= **Replace\_ByELK** &D=somestringhere4

if '&B=' in [body] {  
mutate {  
gsub =\> ['[body]','&B=.\*&','&B=Replace\_ByELK&' ]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [August 9, 2017, 8:14am UTC](https://discuss.elastic.co/t/replace-middle-of-string/96382/2 "2017-08-09T08:14:43Z")

</div>

I suggest `&B=[^&]+`, i.e. "&B" followed by one or more characters of any kind except "&".

---

<div class="post-metadata">

**Author:** ![VincentNgai86](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@VincentNgai86](https://discuss.elastic.co/u/VincentNgai86)\
**Post date:** [August 9, 2017, 9:39am UTC](https://discuss.elastic.co/t/replace-middle-of-string/96382/3 "2017-08-09T09:39:57Z")

</div>

Works fine , thanks

---

<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, 2017, 9:40am UTC](https://discuss.elastic.co/t/replace-middle-of-string/96382/4 "2017-09-06T09:40:06Z")

</div>

Closed on September 6, 2017, 9:40am UTC

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