Adding a regular expression modifier in jenkins pipeline

I would like to run the following regex modifer within my Jenkins pipeline. I have done the following:

pipeline {
options {
disableConcurrentBuilds()
timestamps ()
buildDiscarder(logRotator(numToKeepStr: ‘30’, artifactNumToKeepStr: ‘30’))
timeout(time: 45, unit: ‘MINUTES’)
}
agent {
label “jenkins-slave”
}
libraries {
lib(‘ishared-lib’)
}
stages {
stage(‘Deploy stage’) {
steps {
script {
sh ‘echo’
presetUsersRaw = sh(
script: “”“curl --request GET --url ‘xxxxxxxxxxxx’ --header ‘Accept: application/+json;version=2’ --header ‘Authorization: Token token=xxxxxxxxxxxx’ --header ‘Content-Type: application/json’| jq ‘.users.email’”“”,
returnStdout: true
).trim()
def search = /(\W+)$/gm
echo “${presetUsersRaw}”
presetUsersRaw = presetUsersRaw.replaceAll(‘@domain.com’,‘’)
echo “${presetUsersRaw}”
//presetUsersRaw = presetUsersRaw.replaceAll(’ ‘,’.‘)
//echo “${presetUsersRaw}”
presetUsersRaw = presetUsersRaw.replaceAll(’"', ‘@’)
echo “${presetUsersRaw}”
presetUsersRaw = presetUsersRaw.replaceAll(search , ‘’)
echo “${presetUsersRaw}”
}
}
}
}
}

So I tried adding “gm” to the end of the “search” variable, thinking this would work on jenkins as I tested this out on a regex tester for groovy. However I got a:

groovy.lang.MissingPropertyException: No such property: m for class: groovy.lang.Binding at groovy.lang.Binding.getVariable(Binding.java:63)

Is there anyway to implement global and multiline modifiers? I am fairly new at regex and trying this out as I go.

Just a though, but since you start by making a curl then jq call, why not continue in bash and pipe the result into awk or sed or whatever linux binary you are comfortable with. This way you can quickly iterate and test outside of Jenkins.

1 Like

So I ended up resolving my issue. I was unaware that with Jenkins, or groovy anyway, that we needed to use inline modifiers ahead of the expression. So for example:

This is what I had originally wrote /(\W+)$/gm
This is what I should have written /(?m)(\W+)$/

Although I didn’t have to signify the g to mention that this was global (i’m assuming that this is set by default) I did have to put (?m) ahead of my expression which resolved my issue.

I ended up reading the following documentation: What are regex modifiers, and how to turn them on?

@PierreBtz Thanks anyway for your suggestion!