1
votes

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:testResponse'
def ns3 = 'ns3:testResponse'
def ns2 = 'ns2:testResponse'

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

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

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

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

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

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:testResponse'
def ns3 = 'ns3:testResponse'
def ns2 = 'ns2:testResponse'

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:testResponse'
    def ns3 = 'ns3:testResponse'
    def ns2 = 'ns2:testResponse'

...

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

into something like this:

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

...

if (response.contains(ns)

Sample Response:

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

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

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

<ns3:testResponse
<ns4:testResponse
<ns5:testResponse
<ns6:testResponse
<ns7:testResponse

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

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

1
It would be much simpler if you can attach the sample response and what you are trying to assert than all the above which is not useful at all. - Rao
@Rao I provided the sample response and what I am trying to do, hopefully it makes more sense now? - BruceyBandit
Are you trying XPath match? Or comparing xml? Or something else? Please clarify. - Rao
Just comparing xml via a script assertion - BruceyBandit
It is difficult to image and extract the use case while it can be simple if that is part of the question. Would you mind putting both xml which are comparing? - Rao

1 Answers

2
votes

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)