# Remove over gsub regex

**URL:** https://discuss.elastic.co/t/remove-over-gsub-regex/294875
**Category:** Logstash
**Created:** [January 19, 2022, 9:43pm UTC](https://discuss.elastic.co/t/remove-over-gsub-regex/294875 "2022-01-19T21:43:36Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 19, 2022, 9:43pm UTC](https://discuss.elastic.co/t/remove-over-gsub-regex/294875/1 "2022-01-19T21:43:36Z")

</div>

Hi  
How to fix below code for remove all item with word " term" **from many of field** s,  
one variant of fields

in example line "psm\_info" =\> "7-5-term-term-term-term-term-"

I have tried some like this one, but it doesn't work as expected  
there is something strange with "gsub" I don't have idea what's wrong

```auto
tmp = v.gsub!('-term', '')
tmp = v.gsub!('term', '')
event.set(k, tmp)

```

and at least decided to left regex in gsub:

```auto
ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
      if v.is_a? String 
       if v.include? 'term-'
        event.set(k, v.gsub!('term-',''))
        end
       end
      end
    "
  }

```

```auto
ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
      if v.is_a? String 
       if v.include? '-term'
        event.set(k, v.gsub!('-term',''))
        end
       end
      end
    "
  }

```

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [January 20, 2022, 1:20am UTC](https://discuss.elastic.co/t/remove-over-gsub-regex/294875/2 "2022-01-20T01:20:08Z")

</div>

> [@INS](#):
>
> `tmp = v.gsub!('-term', '')`

what will happen if you use `gsub` rather than `gsub!` in all places?

> <https://stackoverflow.com/questions/612189/why-are-exclamation-marks-used-in-ruby-methods/612196#612196>

---

<div class="post-metadata">

### Author: ![INS](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ins/32/92827_2.png) [@INS](https://discuss.elastic.co/u/INS)
#### Post date: [January 20, 2022, 9:49pm UTC](https://discuss.elastic.co/t/remove-over-gsub-regex/294875/3 "2022-01-20T21:49:36Z")

</div>

Yes, You're right thanks @Tomo_M I've read this article

---

<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: [February 17, 2022, 9:49pm UTC](https://discuss.elastic.co/t/remove-over-gsub-regex/294875/4 "2022-02-17T21:49:37Z")

</div>

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