用java正则表达式替换字符串,如下所述

用java正则表达式替换字符串,如下所述,java,regex,replace,Java,Regex,Replace,您可以尝试以下方法: <#if x == 'abc' > ${add} <#else if y == 'dfg' > bef </#if> 鉴于此: String str = "<#if ${x} || ${z} > ${add} <#else if ${y} == 'dfg' && ${x} == 'abx'> bef <#else if ${y} == 'dfg' > sdadas <#else

您可以尝试以下方法:

 <#if x == 'abc' > ${add} <#else if y == 'dfg' > bef </#if>
鉴于此:

String str = "<#if ${x} || ${z} > ${add} <#else if ${y} == 'dfg' && ${x} == 'abx'> bef <#else if ${y} == 'dfg' > sdadas <#else> ${foo}</#if>";
System.out.println(str.replaceAll("(?<=(?!<#else>)<#|\\|\\||&&)(.+?)\\$\\{(.+?)\\}", "$1$2"));  
收益率:

String str = "<#if ${x} == 'abc' > ${add} <#else if ${y} == 'dfg' > bef </#if>";
System.out.println(str.replaceAll("(<#.+?)\\$\\{(.+?)\\}(.+?>)", "$1$2$3"));     
<#if ${x} || ${z} > ${add} <#else if ${y} == 'dfg' && ${x} == 'abx'> bef <#else if ${y} == 'dfg' > sdadas <#else> ${foo}</#if>

你试过什么了吗?没有,我在考虑使用split,然后添加一些processd字符串…但是在这种情况下,即使${add}也会被更改,我不想这样做,所以我正在尝试,但无法确定需要做什么…非常感谢..这很有效。。但是如果我们有一个像这样的${add}bef sdadas的表达式,其中有两个${}在一个<>,那么它就不起作用了,在任何一个<>,我们可以有n个${}在里面,所以我们必须全部替换..你能告诉我们解决方案吗?我们也可以有任意数量的if-else语句。@user3678399:那你怎么定义else呢?或者你没有这些?可以有,也不可能有其他的那要看情况没有必要有其他的部分
String str = "<#if ${x} || ${z} > ${add} <#else if ${y} == 'dfg' && ${x} == 'abx'> bef <#else if ${y} == 'dfg' > sdadas <#else> ${foo}</#if>";
System.out.println(str.replaceAll("(?<=(?!<#else>)<#|\\|\\||&&)(.+?)\\$\\{(.+?)\\}", "$1$2"));  
<#if ${x} || ${z} > ${add} <#else if ${y} == 'dfg' && ${x} == 'abx'> bef <#else if ${y} == 'dfg' > sdadas <#else> ${foo}</#if>
<#if x || z > ${add} <#else if y == 'dfg' && x == 'abx'> bef <#else if y == 'dfg' > sdadas <#else> ${foo}</#if>