Jose
Jose

Reputation: 1461

Groovy Split not working as expected in Jenkins Pipeline

I'm trying to split a string based on the delimiter "." (DOT) using the groovy split function in a Jenkins pipeline and I would like to assign the split sub strings to multiple variables in a single line. Following is the code that I have (It works in groovy) and I keep getting an error in Jenkins pipeline, what am I doing wrong here? Is there an alternative way to do this? Any pointers are greatly appreciated.

    IP="192.168.234.20"
    def (env.W, env.X, env.Y, env.Z) = IP.split('\\.')
    
    Error:
    WorkflowScript: 543: expecting an identifier, found ',' @ line 543, column 28.
     (env.W, env.X, env.Y, env.Z) = IP.split('\\.')
           ^
    IP="192.168.234.20"
    (env.W, env.X, env.Y, env.Z) = IP.split('\\.')
    
    Error:
    WorkflowScript: 543: expecting ')', found ',' @ line 543, column 24.
     (env.W, env.X, env.Y, env.Z) = IP.split('\\.')
           ^

Upvotes: 1

Views: 10201

Answers (1)

Altaf
Altaf

Reputation: 3076

You need to define the variable IP and you can either use split or tokenize:
Below is a working example:

def IP = "192.168.234.20"
def (W,X,Y,Z) = IP.split('\\.')
                    println(W)
                    println(X)
                    println(Y)
                    println(Z)
// OR
def (A,B,C,D) = IP.tokenize('\\.')
                    println(A)
                    println(B)
                    println(C)
                    println(D)

Upvotes: 6

Related Questions