【问题标题】:Efficient String replace-all strategy in Groovy with "body"Groovy 中使用“body”的高效字符串替换策略
【发布时间】:2012-05-23 10:17:00
【问题描述】:

简单的问题:

这是执行替换的最有效方式,例如:

"Some tekst with a link like this <ref>linktekst</ref>"

"Some tekst with a link like this <a href="linktekst">linktekst</a>"

在 groovy 中?

文本中很容易有多个标签,应该被替换,linktext当然是&lt;ref&gt;标签的主体。

【问题讨论】:

    标签: string performance groovy replace


    【解决方案1】:

    你可以使用String.replaceAll:

    def s = "Some tekst with a link like this <ref>linktekst</ref> and <ref>link2</ref>"
    s = s.replaceAll "<ref>(.+?)</ref>", '<a href="$1">$1</a>'
    

    【讨论】:

    • 像魅力一样工作! :) 不知道 $-placeholder 语法!
    猜你喜欢
    • 2010-09-27
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2019-05-26
    相关资源
    最近更新 更多