我想在 Groovy 中缩进一个多行字符串,但我无法找出正确的 RegEx 语法/或 Regex 标志来实现这一点。
这是我到目前为止的尝试:
def s="""This
is
multiline
"""
println s.replaceAll('/(.*)/'," \1")
println s.replaceAll('/^/'," ")
println s.replaceAll('(?m)/^/'," \1")
println s.replaceAll('(?m)/(.*)/'," \1")
由于某种原因,这些没有按预期工作。
唯一有效的是这个块:
def indented = ""
s.eachLine {
indented = indented + " " + it + "\n"
}
println indented
有没有更短/更有效的方法来缩进 Groovy 中字符串的所有行?
最佳答案
你需要把(?m)
指令 内正则表达式;并且模式是斜线字符串,而不是带斜线的单引号字符串:
s.replaceAll(/(?m)^/, " ")
关于groovy - 使用 Groovy onliner 缩进多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978953/