# Gsub match specific string and replace with blank

**URL:** <https://discuss.elastic.co/t/gsub-match-specific-string-and-replace-with-blank/170225>\
**Category:** Logstash\
**Created:** [February 27, 2019, 6:30pm UTC](https://discuss.elastic.co/t/gsub-match-specific-string-and-replace-with-blank/170225 "2019-02-27T18:30:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arnav\_Sengupta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arnav_sengupta/32/24059_2.png) [@Arnav\_Sengupta](https://discuss.elastic.co/u/Arnav_Sengupta)\
**Post date:** [February 27, 2019, 6:30pm UTC](https://discuss.elastic.co/t/gsub-match-specific-string-and-replace-with-blank/170225/1 "2019-02-27T18:30:55Z")

</div>

I am trying to replace a specific string using gsub but it's not working correctly.

Input -\> /ICW/PDR/backend  
Desired Output -\> /ICW/PDR

What I have used is,

> gsub =\> ["backend\_name", "[\/backend]", ""]

However, it ends up creating this

Output -\> ICWPDR

Can someone please help?

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [February 27, 2019, 7:20pm UTC](https://discuss.elastic.co/t/gsub-match-specific-string-and-replace-with-blank/170225/2 "2019-02-27T19:20:42Z")

</div>

The 2nd argument to gsub is a [Regular Expression](https://www.regular-expressions.info/quickstart.html).

* * *

To remove the last slash and everything that follows it in a sequence, the following pattern should work:

```auto
  gsub => ["backend_name", "/[^/]*$", ""]

```

Explained:

- `/`: a literal forward slash,
- `[^/]*`: followed by a sequence of zero or more characters that are _not_ forward slashes,
- `$`: followed by the end-of-line

* * *

If what follows the backslash is _always_ exactly `backend`, this may be a little more performant:

```auto
  gsub => ["backend_name", "/backend$", ""]

```

Explained:

- `/backend`: a literal forward slash followed by the sequence `b` `a` `c` `k` `e` `n` `d`,
- `$`: followed by the end-of-line

---

<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:** [March 27, 2019, 7:31pm UTC](https://discuss.elastic.co/t/gsub-match-specific-string-and-replace-with-blank/170225/3 "2019-03-27T19:31:59Z")

</div>

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