BruceyBandit BruceyBandit - 1 year ago 98
Groovy Question

How to write wildcards for any character in groovy

I want to know how in groovy scripting can I apply a wildcard character? For example in instead of having a long list like:

def name1 = 'name1'
def name2 = 'name2'
def name3 = 'name3'


I would be happy if the string could be any of those names, so I am not bothered if it's 1,2 or 3, as long as it has a characters after 'name' then it's ok. That means I can have one variable like:

def name = 'name' + wildcard


A little bit like sql where you just want anything beginning where if you want to searrch for something like name..., you would write LIKE 'name%'.

Thanks

Update:

I have three variables:

def ns4 = 'ns4:availabilityResponse'
def ns3 = 'ns3:availabilityResponse'
def ns2 = 'ns2:availabilityResponse'


But instead I want to set a variable for any ns availability tag tag so I tried this:

def ns = 'ns' +/[\d]+/':availabilityResponse'


But I get an Script36.groovy: 17: expecting EOF, found ':availabilityResponse'

I also tried
def ns = 'ns' +/[\d]+/+':availabilityResponse'
but when I perform an assert between ns2 and ns I get this:

assert ns2 == ns | | | | | ns[\d]+:availabilityResponse | false ns2:availabilityResponse


virtually I don't want to provide multiple if statements like so:

if (response.contains(ns2)...
else if (response.contains(ns3)..
else if (response.contains(ns4) etc


I am actually not bothered about the number within either:

def ns4 = 'ns4:availabilityResponse'
def ns3 = 'ns3:availabilityResponse'
def ns2 = 'ns2:availabilityResponse'


So I wanted to implement a wildcard instead of a number so then i can have only one def variable and one if statement rather than 3 def variables and 3 if statements.

So I want to change this:

def ns4 = 'ns4:availabilityResponse'
def ns3 = 'ns3:availabilityResponse'
def ns2 = 'ns2:availabilityResponse'

...

if (response.contains(ns2)...
else if (response.contains(ns3)..
else if (response.contains(ns4) etc


into something like this:

def ns = 'ns' +/[\d]+/':availabilityResponse'

...

if (response.contains(ns)


Sample Response:

<SOAP-ENV:Body>
<ns5:availabilityResponse>
</ns5:availabilityResponse>
</SOAP-ENV:Body>


So in the response above, you can see I have a tag that states:
<ns5:availabilityResponse


However dependent on the request, the number within this tag can change, it could either:

<ns3:availabilityResponse
<ns4:availabilityResponse
<ns5:availabilityResponse
<ns6:availabilityResponse
<ns7:availabilityResponse


Now instead of having if statements for if response contains either:
<ns3:availabilityResponse
or
<ns4:availabilityResponse
or this
<ns5:availabilityResponse
or etc,

I just want to make sure it contains the tag
<ns*:availabilityResponse
where star is any number so it can match any of the 5 tags mentioned above

Rao Rao
Answer Source

In order to identify number in regular expression, use \d. If you are expecting more than one digit, then use \d+.

Here is the sample script:

def list = ['name1','name2', 'name10a']
def pattern = /name[\d]+/
list.collect { assert it ==~ pattern }

If you looks the list, the last element is not in the same pattern as it has character after two digits.

Output:

groovy> def list = ['name1','name2', 'name10a'] 
groovy> def pattern = /name[\d]+/ 
groovy> list.collect { assert it ==~ pattern } 

Exception thrown

Assertion failed: 

assert it ==~ pattern
       |  |   |
       |  |   name[\d]+
       |  false
       name10a

    at ConsoleScript5$_run_closure1.doCall(ConsoleScript5:3)
    at ConsoleScript5.run(ConsoleScript5:3)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download