Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace ${Month} as a string?

I want to create 'custom' placeholder for some variables in my config file. I thought I would use this syntax ${Variable_name}. But I cannot make it work when I want to replace the placeholder with a value. I do not need to print the final value but pass it to another variable. I use println only for debugging. The string variable tmp contains the string that was read from xml config file. So I need tmp2 to have the right string where placeholder was replaced.

String Month = "October"
String tmp = 'This is ${Month} - a month of Love'
String tmp2 = tmp

println tmp2

//println tmp.replaceAll(~/${Month}/,Month)
println tmp.replaceAll("${Month}",Month)   //prints This is ${Month} of Love
println tmp.replaceAll('${Month}',Month)   // throws an error "WARNING: Sanitizing stacktrace:java.util.regex.PatternSyntaxException: Illegal repetition near index 0" 
// desired result to be printed is "This is October

Could someone help me to make it work or understand? I guess I can some other chars to mark the variable. The config file is save as XML.

UPDATE

I hope this code will better explain what I want to achieve

String Month = "October"


// content of the file (c:/tmp/conf.txt) is --> This is ${Month} - a month of Love
// I want f2 to contain "This is October - a month of Love"
// println is not a choice as I don't use println in my code
// I need a variable to contain the final string

def f2 = new File('c:/tmp/conf.txt') //use same path
println f2.text
like image 397
Radek Avatar asked Sep 14 '25 21:09

Radek


2 Answers

You could just use the Java String format method:

String month = 'October'
String tmp = 'This is %s - a month of Love'
String tmp2 = String.format(tmp, month)

Alternately to preserve the use of ${Month} you can use:

import groovy.text.SimpleTemplateEngine
String month = 'October'
String tmp = 'This is ${Month} - a month of Love'
String tmp2 = new SimpleTemplateEngine().createTemplate(tmp).make(Month:month)
like image 117
Dylan Bijnagte Avatar answered Sep 17 '25 19:09

Dylan Bijnagte


If you truly want to use Groovy's GString syntax (shell-like "${var}" variable replacement) against the contents of a file, what you desire is the GStringTemplateEngine. Having said that, the engine itself, unlike the regular compile-time GString facility, does not know anything about your local variable environment, so you have to pass in a map of replacement values when you execute the engine (make() method). Here is what it looks like:

import groovy.text.GStringTemplateEngine

def replacements = [Month:'October']
def file = new File('template.txt')
def engine = new GStringTemplateEngine()
def template = engine.createTemplate(file).make(replacements)
println template

Given the file contents:

This is ${Month} - a month of Love

The following result is printed:

This is October - a month of Love
like image 27
BalRog Avatar answered Sep 17 '25 19:09

BalRog