BruceyBandit
BruceyBandit

Reputation: 4334

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: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

Upvotes: 1

Views: 10747

Answers (1)

Rao
Rao

Reputation: 21379

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)

Upvotes: 2

Related Questions