Jill448
Jill448

Reputation: 1793

simple groovy replaceall regex in a list

I am trying to remove the extra space and (2) in my current list and print the new list. But getting an error for the below code.

def my_list = ["abc (2)","def edf","qwe erfw" ]
def my_new_list = my_list.replaceAll(~/ \([0-9]\)$/, "")
print "my_new_list : ${my_new_list}"

Expected Output

my_new_list : [abc,def edf,qwe erfw]

Error:

Caught: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.replaceAll() is applicable for argument types: (java.util.regex.Pattern, String) values: [ \([0-9]\)$, ]
Possible solutions: replaceAll(java.util.function.UnaryOperator)
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.replaceAll() is applicable for argument types: (java.util.regex.Pattern, String) values: [ \([0-9]\)$, ]
Possible solutions: replaceAll(java.util.function.UnaryOperator)

Upvotes: 1

Views: 3144

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You need to call replace on each element in the list. You can use collect, or the *. operator like so

my_list*.replaceAll(~/ \([0-9]\)$/, "")

Upvotes: 3

Related Questions