<#if { cntry } = "FI" > <#if { addr2 } = " " >" " <#else> ${ addr2 } </#if> ${ addr1 } ${ zipcd } ${ city } <#else> <#if { addr2 } = " " >" " <#else> ${ addr2 } </#if> ${ addr1 } ${ addr2 } ${ addr3 } ${ cntry_desc } ok fine </#if>
我希望输出为
<#if {cntry} = "FI" > <#if {addr2} = " " >" " <#else> ${addr2} </#if> ${addr1} ${zipc} ${city} <#else> <#if {addr2} = " " >" " <#else> ${addr2} </#if> ${addr1} ${addr2} ${addr3} ${cntry_desc} ok fine </#if>
只想删除{}
之间的空格答案 0 :(得分:1)
你可以做到
str = str.replaceAll("\\{\\s*", "{")
.replaceAll("\\s*\\}", "}")
答案 1 :(得分:1)
或者您可以使用
\{\s+([\w]+)\s+\}
并使用
{$1}
作为替代品。
所以,这段代码
String raw = "<#if { cntry } = \"FI\" > <#if { addr2 } = \" \" >\" \" <#else> ${ addr2 } </#if> ${ addr1 } ${ zipcd } ${ city } <#else> <#if { addr2 } = \" \" >\" \" <#else> ${ addr2 } </#if> ${ addr1 } ${ addr2 } ${ addr3 } ${ cntry_desc } ok fine </#if>";
raw.replaceAll("\\{\\s+([\\w]+)\\s+\\}", "{$1}");
将返回
<#if {cntry} = "FI" > <#if {addr2} = " " >" " <#else> ${addr2} </#if> ${addr1} ${zipcd} ${city} <#else> <#if {addr2} = " " >" " <#else> ${addr2} </#if> ${addr1} ${addr2} ${addr3} ${cntry_desc} ok fine </#if>
答案 2 :(得分:0)
只要{
和}
未嵌套,您就可以使用此正则表达式进行搜索:
" +(?=[^{]*})"
并将其用于替换:
""
Java代码:
String repl = input.replaceAll(" +(?=[^{]*})", "");