7

I am very new to using groovy. Especially when it comes to Jenkins+Groovy+Pipelines.

I have a string variable that can change from time to time and want to apply a regex to accomodate the 2 or 3 possible results the string may return.

In my groovy code I have:

r = "Some text that will always end in either running, stopped, starting."
def regex = ~/(.*)running(.*)/
assert regex.matches(r)

But I receive an error in the jenkins output:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.regex.Pattern.matches() is applicable for argument types: (java.lang.String)

UPDATE: I was able to create a pretty nifty jenking groovy while loop in a pipeline job i am creating to wait for a remote process using the regex info here and a tip in a different post (How do I iterate over all bytes in an inputStream using Groovy, given that it lacks a do-while statement?).

            while({
                def r = sh returnStdout: true, script: 'ssh "Insert your remote ssh command that returns text'
                println "Process still running.  Waiting on Stop"
                println "Status returned: $r"
                r =~ /running|starting|partial/
            }());
mrzasa
  • 22,895
  • 11
  • 56
  • 94
JuanD
  • 391
  • 1
  • 6
  • 13
  • I ended up taking out the 'assert' line and just doing 'r =~ /running|starting|stopped/' thanks @injecteer – JuanD Dec 21 '16 at 15:42

3 Answers3

9

Straight-forward would be:

String r = "Some text that will always end in either running, stopped, starting."
assert r =~ /(.*)running(.*)/
injecteer
  • 20,038
  • 4
  • 45
  • 89
3

If you are only using this regex here you can try the following:

r = "Some text that will always end in either running, stopped, starting."
assert r ==~ /(.*)(running|stopped|starting)\.?$/, "String should end with either running, started or stopped" 

Explanation:

(.*) - matches anything
(running|stopped|starting) - matches either running, stopped or starting
\.? - optionally end with a dot expect zero or one occurrence of a dot, but you need to escape it, because the dot is a regex special character
$ - end of the line, so nothing should come after

the ==~ operator is the groovy binary match operator. This will return true if it matches, else false

See this example on regex 101

Rik
  • 3,647
  • 2
  • 25
  • 34
  • Thank you for the thorough explanation and links. In the end, using '=~' instead of '==~' worked for me in the way i wanted to use this code – JuanD Dec 21 '16 at 15:49
  • 1
    In your case the result is the same. However ‘=~‘ will return a regex object, which is `null` when there is no match. `assert null` will assert. Therefore it will work as well. Since you are not using the object you do not necessarily need the object. A Boolean will suffice. Don't know about performance and memory differences, although in this case they will be very small. The `==~` will return `true` on the first match and will therefor take the same time in the worst case. – Rik Dec 21 '16 at 17:25
  • But glad it helped. Regex101 is a great site if you want to validate regular expressions btw – Rik Dec 21 '16 at 17:25
  • Great explanation! You saved my day. Thank you. – Sandeep Amarnath Dec 16 '20 at 16:42
0

matches does not receive String.

Try

Pattern.compile("your-regex").matcher("string-to-check").find()
Amityo
  • 5,635
  • 4
  • 22
  • 29
  • 1
    That does require explicit approval. Otherwise it will fail: org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.util.regex.Matcher find – Rob Spoor May 02 '19 at 13:01