regex - 尝试在 Jenkinsfile 中的 '//' 上拆分字符串,而不是在 '/' 上拆分
问题描述
//
在 a中的双正斜杠上标记字符串的正确方法是Jenkinsfile
什么?
下面的示例导致字符串在单正斜杠上被标记/
,这不是所需的行为。
詹金斯文件
Jenkinsfile
包含相关部分的 一个简短的、过度简化的示例是:
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
def theURL = "http://<ip-on-lan>:<port-num>/path/to/some.asset"
sh "echo 'theURL is: ${theURL}'"
def tokenizedURL = theURL.tokenize('//')
sh "echo 'tokenizedURL is: ${tokenizedURL}'"
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
日志:
前面的日志输出是:
echo 'theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset'— Shell Script<1s
[ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
+ echo theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset
theURL is: http://<ip-on-lan>:<port-num>/path/to/some.asset
echo 'tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]'— Shell Script<1s
[ne_Branch-Name-M2X23QGNMETLDZWFK7IXVZQRCNSWYNTDFJZU54VP7DMIOD6Z4DGA] Running shell script
+ echo tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]
tokenizedURL is: [http:, <ip-on-lan>:<port-num>, path, to, some.asset]
请注意,日志显示字符串正在 tokeni on/
而不是 on //
。
解决方案
tokenize
将字符串作为可选参数,可以包含 1 个或多个字符作为分隔符。它将字符串参数中的每个字符视为单独的分隔符,因此//
实际上与/
要拆分//
,您可以使用split
支持正则表达式的:
theURL.split(/\/{2}/)
推荐阅读
- angular - InvalidPipeArgument: '2017-12-05 05:30:00 for pipe 'DatePipe' - Safari
- java - 作为 Java 对象接收来自 REST 客户端的响应
- python - 约束python函数参数
- java - 在矩阵中查找元素之和为给定数字的路径
- c# - 数据绑定更新但视图不是
- asp.net-core - 如何丰富 DependencyTelemetry?
- python - 如何在 Windows 上修复错误“ImportError: No module named 'selenium'”?
- python - 发送带有虚拟显示的键时,Selenium (python) 崩溃
- javascript - 如何在客户端减少 JSON.stringify 使用的内存量?
- android - 在一个列表项中创建具有各种 UI 组件的自定义 Recycler 列表视图