DEV Community

ouryperd
ouryperd

Posted on

Remove items matching a pattern from a list in Groovy

Here is how you can remove items from a list that match a text pattern.

myList = ["lice",
          "mice",
          "dice",
          "ice",
          "mosquito",
          "rice",
          "apple",
          "thrice"]

myList.removeAll {it -> it.contains("ice")}

assert myList == ["mosquito", "apple"]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)