高效的字符串替换 - Groovy中的所有策略与“body”

时间:2012-05-23 10:17:01

标签: string performance groovy replace

简单问题:

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

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

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

在groovy?

文本中很容易有多个标记,应该替换, linktext 当然是<ref>标记的主体。

1 个答案:

答案 0 :(得分:2)

您可以使用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>'