Groovy:过滤掉"Key:Value"对并存储在数组中的正则表达式
Groovy: regex to filter out "Key:Value" pair and store in an array
我正在为 Jenkinsfile 编写一个 GROOVY 脚本来执行以下操作:
第一步:读取输入文件info_file.txt
。信息文件内容如下:
sh> cat info_file.txt
CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0
Step-2:将CHIP_DETAILS
去掉后缀-A0,-B1
等后存入数组。
例如:
Integer[] testArray = ["1234", "1456", "2456" , "3436" , "4467"]
Step-3:顺序打印数组的元素。例如:
testArray.each {
println "chip num is ${it}"
}
我写了一个小代码来测试 CHIP_DETAILS
'key' 是否存在于输入文件中并且它正在工作。
但是我发现很难编写用于删除后缀(-A0,-B1
等)的正则表达式并将相应的值存储在数组中。
代码如下:
stage('Read_Params')
{
steps
{
script
{
println ("Let's validate and see whether the "Key:Value" is present in the info_file.txt\n");
if ( new File("$JobPath/Inputparams.txt").text?.contains("CHIPS_DETAILS"))
{
println ("INFO: "Key:Value" is present in the info_file.txt \n");
>>>>> proceed with Step-1, Step-2 and Step-3... <<<<<
}
else
{
println ("WARN: "Key:Value" is NOT present in the info_file.txt \n");
exit 0;
}
}
}
}
在此先感谢您的帮助!
您可以尝试以下操作:
def chipDetails = 'CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0'
def testArray = ( chipDetails =~ /(?<=^|[:;])\d*(?=[-])/)
assert '1234' == testArray[0]
assert '1456' == testArray[1]
assert '2456' == testArray[2]
assert '3436' == testArray[3]
assert '4467' == testArray[4]
正则表达式可确保您尝试捕获的数字位于行首或前缀为 : 或 ;并以 -
为后缀
分隔符之间可以有任意数量的数字。
迭代结果如下:
testArray.each{
println it
}
我正在为 Jenkinsfile 编写一个 GROOVY 脚本来执行以下操作:
第一步:读取输入文件info_file.txt
。信息文件内容如下:
sh> cat info_file.txt
CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0
Step-2:将CHIP_DETAILS
去掉后缀-A0,-B1
等后存入数组。
例如:
Integer[] testArray = ["1234", "1456", "2456" , "3436" , "4467"]
Step-3:顺序打印数组的元素。例如:
testArray.each {
println "chip num is ${it}"
}
我写了一个小代码来测试 CHIP_DETAILS
'key' 是否存在于输入文件中并且它正在工作。
但是我发现很难编写用于删除后缀(-A0,-B1
等)的正则表达式并将相应的值存储在数组中。
代码如下:
stage('Read_Params')
{
steps
{
script
{
println ("Let's validate and see whether the "Key:Value" is present in the info_file.txt\n");
if ( new File("$JobPath/Inputparams.txt").text?.contains("CHIPS_DETAILS"))
{
println ("INFO: "Key:Value" is present in the info_file.txt \n");
>>>>> proceed with Step-1, Step-2 and Step-3... <<<<<
}
else
{
println ("WARN: "Key:Value" is NOT present in the info_file.txt \n");
exit 0;
}
}
}
}
在此先感谢您的帮助!
您可以尝试以下操作:
def chipDetails = 'CHIP_DETAILS:1234-A0;1456-B1;2456-B0;3436-D0;4467-C0'
def testArray = ( chipDetails =~ /(?<=^|[:;])\d*(?=[-])/)
assert '1234' == testArray[0]
assert '1456' == testArray[1]
assert '2456' == testArray[2]
assert '3436' == testArray[3]
assert '4467' == testArray[4]
正则表达式可确保您尝试捕获的数字位于行首或前缀为 : 或 ;并以 -
为后缀分隔符之间可以有任意数量的数字。
迭代结果如下:
testArray.each{
println it
}