# Ruby Filter to delete array element with regexp

**URL:** <https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131>\
**Category:** Logstash\
**Created:** [August 28, 2019, 2:04pm UTC](https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131 "2019-08-28T14:04:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_Paiva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago_paiva/32/52326_2.png) [@Thiago\_Paiva](https://discuss.elastic.co/u/Thiago_Paiva)\
**Post date:** [August 28, 2019, 2:04pm UTC](https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131/1 "2019-08-28T14:04:28Z")

</div>

Hi, I try to delete an array element using a regexp, But code isn't run.

Imput json:

```
`{"test":"The http module allows to expose\nThe http module allows to expose\ncamp:using asynchronous communication\nusing asynchronous communication"}`

```

In this case, I want to delete any element that has "http module" in value.

Pipiline:

```
input{
    stdin{
        codec => json {
         } 
    }
}

filter{

    ruby{
        code => '
           matches = event.get("test").split("\n")
            matches.each_index{ |x|
            if matches[x] =~ ".*http module.*"
            else 
               matches.delete_at(x)
            end
           }
           matches.each_index{ |x|
           event.set("line#{x+1}", matches[x])
           }
        '   
    }  
        
    
}
output {
    stdout {
        codec => rubydebug
    } 
}

```

I want the result:

```
line1:using asynchronous communication
line1:using asynchronous communication
```

---

<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:** [August 28, 2019, 2:36pm UTC](https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131/2 "2019-08-28T14:36:12Z")

</div>

You cannot modify an array using delete\_at whilst iterating over it

```
    code => '
        matches = event.get("test").split("\n")
        matches = matches.delete_if { |x| x =~ /http module/ }
        matches.each_index{ |x|
            event.set("line#{x+1}", matches[x])
        }
    '
```

---

<div class="post-metadata">

**Author:** ![Thiago\_Paiva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago_paiva/32/52326_2.png) [@Thiago\_Paiva](https://discuss.elastic.co/u/Thiago_Paiva)\
**Post date:** [August 28, 2019, 2:55pm UTC](https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131/3 "2019-08-28T14:55:27Z")

</div>

Thanks for your help. Working perfectly!

---

<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 25, 2019, 2:55pm UTC](https://discuss.elastic.co/t/ruby-filter-to-delete-array-element-with-regexp/197131/4 "2019-09-25T14:55:28Z")

</div>

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