Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy 如何将${Month}替换为字符串?_Groovy_Str Replace_Placeholder - Fatal编程技术网

Groovy 如何将${Month}替换为字符串?

Groovy 如何将${Month}替换为字符串?,groovy,str-replace,placeholder,Groovy,Str Replace,Placeholder,我想为配置文件中的某些变量创建“自定义”占位符。我想我会使用这种语法${Variable\u name}。但是当我想用一个值替换占位符时,我无法使它工作。我不需要打印最终值,而是将其传递给另一个变量。我只使用println进行调试。字符串变量tmp包含从xml配置文件读取的字符串。所以我需要tmp2在占位符被替换的地方有一个正确的字符串 String Month = "October" String tmp = 'This is ${Month} - a month of Love' Strin

我想为配置文件中的某些变量创建“自定义”占位符。我想我会使用这种语法
${Variable\u name}
。但是当我想用一个值替换占位符时,我无法使它工作。我不需要打印最终值,而是将其传递给另一个变量。我只使用println进行调试。字符串变量tmp包含从xml配置文件读取的字符串。所以我需要tmp2在占位符被替换的地方有一个正确的字符串

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
有人能帮我使它工作或理解吗?我想我可以用其他字符来标记变量。配置文件另存为XML

更新

我希望这段代码能更好地解释我想要实现的目标

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

您可以使用Java方法:

或者,要保留
${Month}
的使用,您可以使用:

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)

您可以使用Java方法:

或者,要保留
${Month}
的使用,您可以使用:

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)

最简单的方法可能是:

String Month = 'October'
String tmp = 'This is ${Month} - a month of Love'
String tmp2 = tmp.replaceAll(/\$\{Month\}/, Month)

assert tmp2 == 'This is October - a month of Love'

//alternate test
def f2 = new File('~/dump.txt') //use same path
String tmp3 = f2.text.replaceAll(/\$\{Month\}/, Month)
println tmp3 //prints This is October - a month of Love
在您提供的第一个示例中,您的代码中的问题是Groovy正在采取行动

println tmp.replaceAll("${Month}",Month)
并将其内部转换为

println tmp.replaceAll("October",Month)

。当Groovy捕获双引号文本时,其中包含一个
${}
(我可以说是一个字符串,但实际上是一个GString),它会替换
${}
中名为的变量。使用正则表达式可以避免这种情况,因为在正则表达式上没有变量替换。

最简单的方法可能是:

String Month = 'October'
String tmp = 'This is ${Month} - a month of Love'
String tmp2 = tmp.replaceAll(/\$\{Month\}/, Month)

assert tmp2 == 'This is October - a month of Love'

//alternate test
def f2 = new File('~/dump.txt') //use same path
String tmp3 = f2.text.replaceAll(/\$\{Month\}/, Month)
println tmp3 //prints This is October - a month of Love
在您提供的第一个示例中,您的代码中的问题是Groovy正在采取行动

println tmp.replaceAll("${Month}",Month)
并将其内部转换为

println tmp.replaceAll("October",Month)

。当Groovy捕获双引号文本时,其中包含一个
${}
(我可以说是一个字符串,但实际上是一个GString),它会替换
${}
中名为的变量。使用正则表达式可以避免这种情况,因为在正则表达式上没有变量替换。

如果您确实想对文件内容使用Groovy的
GString
语法(类似于shell的
“${var}”
变量替换),那么您需要的是
GStringTemplateEngine
。话虽如此,与常规编译时
GString
工具不同,引擎本身对局部变量环境一无所知,因此在执行引擎时必须传入替换值的映射(
make()
方法)。下面是它的外观:

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
鉴于文件内容:

This is ${Month} - a month of Love
将打印以下结果:

This is October - a month of Love

如果您确实想对文件内容使用Groovy的
GString
语法(类似于shell的
“${var}”
变量替换),那么您需要的是
GStringTemplateEngine
。话虽如此,与常规编译时
GString
工具不同,引擎本身对局部变量环境一无所知,因此在执行引擎时必须传入替换值的映射(
make()
方法)。下面是它的外观:

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
鉴于文件内容:

This is ${Month} - a month of Love
将打印以下结果:

This is October - a month of Love

但是占位符是
${Month}
,不是
%s
,但是占位符是
${Month}
,不是
%s
,所以基本上,我没有正确地逃脱
${Month}
。我提供了两个版本,但我认为
println tmp.replaceAll('${Month}',Month)
可以工作。这里发生的事情是,您的字符串被解释为正则表达式。只是这一次,它没有合适的反斜杠来转义特殊字符,因此正则表达式引擎将
{}
视为重复量化符。因此,regex最后说了一些类似“查找一系列$字符,连续重复一个月”的话,这是没有意义的。但是
tmp.replaceAll('${Month}',Month)
我认为“${Month}将被视为stringit。但是,检查-第一个字符串被解释为regex。所以基本上,我没有逃避
${Month}
正确。我提供了两个版本,但我认为
println tmp.replaceAll('${Month}',Month)
会起作用。是的,我对此有点困惑。这里发生的事情是,您的字符串被解释为正则表达式。只是这一次,它没有合适的反斜杠来转义特殊字符,所以正则表达式引擎将
{}
视为重复量化符。因此,正则表达式最终会说查找一系列$字符,在一行中重复一个月”,这没有意义。但是
tmp.replaceAll('${Month}',Month)
我认为“${Month}将被视为stringit。但是,检查-第一个字符串被解释为正则表达式。