WhoAmI
WhoAmI

Reputation: 1133

Do not show match that contains a string

Guys I have the following case:

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

How to exclude from this regex strings that have ” something ” or whatever it will be in it's place inside it so it will print only company test and DPDHLPA

Expected result : PRINT company-test and mytest-1234 and NOT "company-something"

Upvotes: 1

Views: 59

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626926

You can use

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

Note that the .findAll is run directly on the string array where the regex is updated with a negative lookahead to avoid matching any strings where something comes directly after company-.

See the Groovy demo.

Upvotes: 1

Related Questions