不显示包含字符串的匹配项

Do not show match that contains a string

伙计们,我有以下情况:

def AllEnviroments = ['company-test', 'MYTEST-1234', 'company-somethingelse-something']
def EnviromentChoices = (AllEnviroments =~  /company-.*|MYTEST/).findAll().collect().sort()

如何从此正则表达式中排除具有“某物”的字符串或将在其中放置的任何内容,以便它仅打印公司测试和 DPDHLPA

预期结果: 打印 company-test 和 mytest-1234 和 NOT“公司某事”

您可以使用

def AllEnviroments = ['company-test', 'MYTEST-1234', 'company-somethingelse-something']
def EnviromentChoices = AllEnviroments.findAll { it =~ /company-(?!something).*|MYTEST/ }.sort()
print(EnviromentChoices)
// => [MYTEST-1234, company-test]

请注意,.findAll 直接在字符串数组上 运行,其中正则表达式使用负前瞻更新,以避免匹配任何 something 紧接在 [=13 之后的字符串=].

参见Groovy demo