Hoof
Hoof

Reputation: 1758

Efficient String replace-all strategy in Groovy with "body"

Simple question:

Which is the most efficient way of performing a substituion like:

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

To

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

In groovy?

There can easily be more than one tag in the text, that should be substituted, and linktext is of course the body of the <ref> tag.

Upvotes: 2

Views: 1145

Answers (1)

tim_yates
tim_yates

Reputation: 171194

You can use 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>'

Upvotes: 2

Related Questions