5

I wonder if there is any groovy-way to check if substring of strings matches to patterns.

For example I have strings List (or array):

def Errors = ['File xyz cannot be created: No space left on device', 'File kjh has errors: some_error']

Then I have list of strings, for example def Patterns = ['Tests failed', 'No space left on device', 'Something goes wrong', ...some strings... ]

I would like to check if some elements of List Patterns are substrings of Errors elements .

In that example it should return true, because Patterns has No space left on device and Errors has 'File xyz cannot be created: No space left on device'.

I know how to write it very ulgy and not efficient by using two for loops and method contains, but I know that Groovy has much more powerfull built-in methods. I have tried with findAll(), but it doesnt worked at all.

Do you have any ideas? Is there any way to make it more clever?

1 Answer 1

4

Explicitly naming pattern and error:

patterns.find { pattern -> errors.find { error -> error.contains(pattern) } }  // -> No space left on device
patterns.any { pattern -> errors.find { error -> error.contains(pattern) } } // -> true

depending on what/how many you want to find.

Or even shorter:

patterns.find { errors.find { error -> error.contains(it) } }
patterns.any { errors.find { error -> error.contains(it) } }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.