在以下声明性语法管道中:
pipeline {
agent any
stages {
stage( "1" ) {
steps {
script {
orig = "/path/to/file"
two_lev_down = (orig =~ /^(?:\/[^\/]*){2}(.*)/)[0][1]
echo "${two_lev_down}"
depth = 2
two_lev_down = (orig =~ /^(?:\/[^\/]*){depth}(.*)/)[0][1]
echo "${two_lev_down}"
}
}
}
}
}
...正则表达式旨在匹配“ /
”的第三个实例之后的所有内容。
第一个,即(orig =~ /^(?:\/[^\/]*){2}(.*)/)[0][1]
作品。
但第二,(orig =~ /^(?:\/[^\/]*){depth}(.*)/)[0][1]
没有。它生成此错误:
java.util.regex.PatternSyntaxException: Illegal repetition near index 10
^(?:/[^/]*){depth}(.*)
我认为问题是使用变量depth
而不是硬编码整数,因为这是工作代码和错误生成代码之间的唯一区别。
如何Groovy
在正则表达式模式 find-count 中使用变量?或者Groovy
编写在第 n 次出现模式后返回所有内容的正则表达式的-language 惯用方法是什么?
您缺少$
变量前面的 。它应该是:
orig = "/path/to/file"
depth = 2
two_lev_down = (orig =~ /^(?:\/[^\/]*){$depth}(.*)/)[0][1]
assert '/file' == two_lev_down
为什么?
在 Groovy 中,字符串插值(over GString
)适用于 3 个字符串文字:
"Hello $world, my name is ${name.toUpperCase()}"
Slashy
-strings 通常用作正则表达式文字: /.{$depth}/
def email = """
Dear ${user}.
Thank your for blablah.
"""
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句