什么字符串替换Java API';是否可以使用s(模板库)执行条件和自定义字符串重放?

什么字符串替换Java API';是否可以使用s(模板库)执行条件和自定义字符串重放?,java,substitution,Java,Substitution,我正在寻找一个开源JavaAPI,它允许我基于自定义标记进行可配置字符串替换 模板示例: 我已经有了一个部分使用正则表达式和正则字符串替换构建的解决方案,但是我希望有一个更强大的预构建解决方案 我看过Commons Lang3的StrSubstitutor,虽然它可以处理简单和自定义的替换,但似乎没有更多语法驱动的替换 更新#1 目前仍停留在Java1.6上。我认为@TinkerTenorSoftwareGuy关于模板库的建议是最好的选择。有很多,我用一点 基本上,您有一个模板: You did

我正在寻找一个开源JavaAPI,它允许我基于自定义标记进行可配置字符串替换

模板示例: 我已经有了一个部分使用正则表达式和正则字符串替换构建的解决方案,但是我希望有一个更强大的预构建解决方案

我看过Commons Lang3的StrSubstitutor,虽然它可以处理简单和自定义的替换,但似乎没有更多语法驱动的替换

更新#1
目前仍停留在Java1.6上。

我认为@TinkerTenorSoftwareGuy关于模板库的建议是最好的选择。有很多,我用一点

基本上,您有一个模板:

You did ${action} at ${date} in ${city} ${state} ${zip}. Yours truly, ${firstName} ${lastName}.
以及包含以下数据的模型(java类):

class MyTemplate extends StringTemplate {

    public MyTemplate(String action, Date date, /* etc */ ) { /* set the model state */ }

    public String getTemplateFileLocation() { /* point to the template file */ }

    public String process() { /* process the template and return the string */ }

    public String getAction() { /* return the action as a string */ }

    public String getDate() { /* return the formatted date as a string, i.e. */ 
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        return df.format(date);
    }

    public String getCity() { /* return the city as a string */ }

    public String getState() { /* return the state as a string */ }

    public String getZip() { /* return the zip code as a string */ }

    public String getFirstName() { /* return the first name as a string */ }

    public String getLastName() { /* return the last name as a string */ }
}
然后在代码中可以实例化模板并对其进行处理。处理模板将模板中的
${firstName}
实例替换为模型中的
getFirstName()
返回值(对于每个变量依此类推):

现在
letter
包含用模型中的值填充的模板


有很多不同的模板库,但这是基本的想法。

听起来您正在寻找一个模板库。你试过Thymeleaf吗?Java9有一个可以提交函数的模板库。是的,我正在寻找一个模板库。不幸的是,目前还不能升级到Java9。在客户端批准我们升级之前,我们目前只能使用Java6。按照建议查看ThymileAF和Freemarker,看看它们是否符合我的要求。如果值为null,是否允许剥离文本子集?例如:对于类似于
[在{CITY},{STATE}]
的内容,如果其中一个值为null,我希望防止删除方括号(或等效值)之间的所有内容。注意:“in”和“,”需要在每个模板的基础上在数据库中进行配置。当然,您可以在模型中完成所有这些工作-它只是Java,按照您喜欢的方式转换文本,模板本身应该非常简单。
You did ${action} at ${date} in ${city} ${state} ${zip}. Yours truly, ${firstName} ${lastName}.
class MyTemplate extends StringTemplate {

    public MyTemplate(String action, Date date, /* etc */ ) { /* set the model state */ }

    public String getTemplateFileLocation() { /* point to the template file */ }

    public String process() { /* process the template and return the string */ }

    public String getAction() { /* return the action as a string */ }

    public String getDate() { /* return the formatted date as a string, i.e. */ 
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        return df.format(date);
    }

    public String getCity() { /* return the city as a string */ }

    public String getState() { /* return the state as a string */ }

    public String getZip() { /* return the zip code as a string */ }

    public String getFirstName() { /* return the first name as a string */ }

    public String getLastName() { /* return the last name as a string */ }
}
StringTemplate template = new MyTemplate(action, date, city, state, zip, firstName, lastName);
String letter = template.process();