如何检查字符串数组的子字符串是否与其他字符串数组的模式匹配
How to check if substring of string's array match to patterns from other string array
我想知道是否有任何 groovy 方法来检查字符串的子字符串是否与模式匹配。
例如我有字符串列表(或数组):
def Errors = ['File xyz cannot be created: No space left on device', 'File kjh has errors: some_error']
然后我有字符串列表,例如 def Patterns = ['Tests failed', 'No space left on device', 'Something goes wrong', ...some strings... ]
我想检查列表 Patterns
的某些元素是否是 Errors
元素的子串。
在那个例子中它应该 return 为真,因为 Patterns
有 No space left on device
而 Errors
有 'File xyz cannot be created: No space left on device'
.
我知道如何通过使用两个 for 循环和方法 contains
来编写非常丑陋且效率不高的方法,但我知道 Groovy 具有更强大的内置方法。我试过 findAll()
,但根本不起作用。
你有什么想法吗?有什么办法让它更聪明吗?
明确命名 pattern
和 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
取决于 what/how 个您想查找的数量。
或更短:
patterns.find { errors.find { error -> error.contains(it) } }
patterns.any { errors.find { error -> error.contains(it) } }
我想知道是否有任何 groovy 方法来检查字符串的子字符串是否与模式匹配。
例如我有字符串列表(或数组):
def Errors = ['File xyz cannot be created: No space left on device', 'File kjh has errors: some_error']
然后我有字符串列表,例如 def Patterns = ['Tests failed', 'No space left on device', 'Something goes wrong', ...some strings... ]
我想检查列表 Patterns
的某些元素是否是 Errors
元素的子串。
在那个例子中它应该 return 为真,因为 Patterns
有 No space left on device
而 Errors
有 'File xyz cannot be created: No space left on device'
.
我知道如何通过使用两个 for 循环和方法 contains
来编写非常丑陋且效率不高的方法,但我知道 Groovy 具有更强大的内置方法。我试过 findAll()
,但根本不起作用。
你有什么想法吗?有什么办法让它更聪明吗?
明确命名 pattern
和 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
取决于 what/how 个您想查找的数量。
或更短:
patterns.find { errors.find { error -> error.contains(it) } }
patterns.any { errors.find { error -> error.contains(it) } }