Extracting part of a string on jenkins pipeline

前端 未结 1 985
醉酒成梦
醉酒成梦 2021-01-13 11:26

I am having some trouble with the syntax in my pipeline script.

I am trying to capture everything after the last forward slash \"/\" and before the last period \".\"

相关标签:
1条回答
  • 2021-01-13 12:16

    In this case, it looks like using a few Groovy/Java methods on the String can extract the parts.

    final beforeColon = url.substring(0, url.indexOf(':'))  // git@github.com
    final afterLastSlash = url.substring(url.lastIndexOf('/') + 1, url.length()) // project/access-server-pd.git
    

    This uses a few different methods:

    • public int String.indexOf(String str, int fromIndex)
    • public String String.substring(int beginIndex, int endIndex)
    • public int String.length()
    • public int String.lastIndexOf(String str)

    You do need to be careful about the code you use in your pipeline. If it is sandboxed it will run in a protected domain where every invocation is security checked. For example, the whitelist in the Script Security Plugin whitelists all of the calls used above (for example, method java.lang.String lastIndexOf java.lang.String).

    Performing String manipulation in your pipeline code is perfectly reasonable as you might make decisions and change your orchestration based on it.

    0 讨论(0)
提交回复
热议问题