/ / Jenkins and Groovy and Regex - Jenkins, groovy

Jenkins i Groovy i Regex - Jenkins, groovy

Jestem bardzo nowy w używaniu groovy. Zwłaszcza jeśli chodzi o Jenkins + Groovy + Pipelines.

Mam zmienną ciąg, która może się zmieniać od czasu do czasu i chcę zastosować wyrażenie regularne, aby dostosować 2 lub 3 możliwe wyniki, które ciąg może zwrócić.

W moim świetnym kodzie mam:

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

Ale pojawia się błąd w danych wyjściowych Jenkinsa:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: Brak podpisu metody: java.util.regex.Pattern.matches () ma zastosowanie do typów argumentów: (java.lang.String)

AKTUALIZACJA: Byłem w stanie stworzyć całkiem fajny, jenking groovy, podczas gdy pętla w zadaniu potoku tworzę, aby czekać na zdalny proces przy użyciu informacji o wyrażeniu regularnym tutaj i napiwku w innym poście (do .. while () w Groovy z inputStream?).

            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/
}());

Odpowiedzi:

0 dla odpowiedzi № 1

Prosto byłoby:

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

0 dla odpowiedzi nr 2

mecze nie otrzymuje ciągu.

Próbować

Pattern.compile("your-regex").matcher("string-to-check").find()

0 dla odpowiedzi № 3

Jeśli używasz tylko tego wyrażenia regularnego tutaj, możesz wypróbować następujące czynności:

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"

Wyjaśnienie:

(.*) - 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

temu ==~ operator jest groovy operator dopasowania binarnego. To wróci true jeśli pasuje, w przeciwnym razie false

Widzieć ten przykład na regex 101