Friday, 6 September 2013

Swap string field values in Groovy with inline regex?

Swap string field values in Groovy with inline regex?

I have a POJO:
public class Dog {
String uuid;
// ...etc.
}
I would like to write a simple Groovy expression that replaces instances
of certain characters in the uuid string field with other chars/strings,
specifically:
Replace instances of "@" with the word "elbow"
Replace instances of double-quote """ with a single-quote ("'")
Replace instances of "%" with the word "shoulder"
I know the Groovy will be something like:
myDog.getUUID().replaceAll("[@|\"|%]+", ???);
But I can't figure out how to do all 3 inline, as well as what their
actual regexes should be.
If possible I'd like it to be one single replace command and not 3
separate ones, but if a single command isn't possible I'll take 3
individual replaces. Thanks in advance!

No comments:

Post a Comment