java解析{{}}變量名以及文本內(nèi)容替換操作
發(fā)短信、發(fā)郵件的時候經(jīng)常會遇到模板內(nèi)容需要替換成實(shí)際數(shù)據(jù)的問題,本文介紹從文本模板中解析出變量列表,以及參數(shù)填入后得到實(shí)際文本內(nèi)容的辦法:
/**
* 根據(jù)正則表達(dá)式獲取文本中的變量名列表
* @param pattern
* @param content
* @return
*/
public static List<String> getParams(String pattern, String content) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(content);
List<String> result = new ArrayList<String>();
while (m.find()) {
result.add(m.group(1));
}
return result;
}
/**
* 根據(jù)正則表達(dá)式將文本中的變量使用實(shí)際的數(shù)據(jù)替換成無變量的文本
* @param pattern
* @param content
* @param data
* @return
*/
public static String parse(String pattern, String content, Map<String, String> data) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(content);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String key = m.group(1);
String value = data.get(key);
m.appendReplacement(sb, value == null ? "" : value);
}
m.appendTail(sb);
return sb.toString();
}
public static void main(String[] args) {
String content = "恭喜{{姓名}}報(bào)名成功,請憑報(bào)名編號{[code]}到現(xiàn)場參加活動";
String reg = "\\{\\{(.+?)\\}\\}";
List<String> params = getParams(reg, content);
System.out.println(params);
Map<String, String> data = new HashMap<String, String>();
data.put("姓名", "張三豐");
data.put("code", "930118");
String text = parse(reg, content, data);
System.out.println(text);
}
上面的代碼介紹的是針對{{}}形式的變量值的解析辦法,大家可以修改一下正則表達(dá)式,改成可以解析${}變量的辦法
補(bǔ)充知識:java模板字符串優(yōu)雅解析(占位符解析)
項(xiàng)目中常常需要解析字符串模板,比如user:{userId}:{userType}用于redis的key等,比較常見的做法就是使用String.format(“user:%s:%s”, 1, 1)方法,但個人感覺那樣的模板不夠明了,而使用模板解析器可更好地有助于解析此類字符串。
可使用map用于解析,也可使用對象進(jìn)行解析,也可使用類似String.format可變參數(shù)進(jìn)行解析,多樣化解析對象值。有點(diǎn)類似于js的模板字符串${}。
也可自定義前綴后綴進(jìn)行解析。如PlaceholderResolver.getResolver("{", “}”)該對象可解析{}該類型的占位符.
性能方面:


PlaceholderResolve解析結(jié)果
String.format解析結(jié)果
解析100w次,占位符解析比String.format平均都會快個0.4~0.5s,so 性能應(yīng)該問題不大。最主要的是不需要調(diào)用太多的對象方法,自動解析,方便快捷。
源代碼如下:
/**
* 占位符解析器
*
* @author meilin.huang
* @version 1.0
* @date 2018-11-13 1:42 PM
*/
public class PlaceholderResolver {
/**
* 默認(rèn)前綴占位符
*/
public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
/**
* 默認(rèn)后綴占位符
*/
public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
/**
* 默認(rèn)單例解析器
*/
private static PlaceholderResolver defaultResolver = new PlaceholderResolver();
/**
* 占位符前綴
*/
private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
/**
* 占位符后綴
*/
private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
private PlaceholderResolver(){}
private PlaceholderResolver(String placeholderPrefix, String placeholderSuffix) {
this.placeholderPrefix = placeholderPrefix;
this.placeholderSuffix = placeholderSuffix;
}
/**
* 獲取默認(rèn)的占位符解析器,即占位符前綴為"${", 后綴為"}"
* @return
*/
public static PlaceholderResolver getDefaultResolver() {
return defaultResolver;
}
public static PlaceholderResolver getResolver(String placeholderPrefix, String placeholderSuffix) {
return new PlaceholderResolver(placeholderPrefix, placeholderSuffix);
}
/**
* 解析帶有指定占位符的模板字符串,默認(rèn)占位符為前綴:${ 后綴:}<br/><br/>
* 如:template = category:${}:product:${}<br/>
* values = {"1", "2"}<br/>
* 返回 category:1:product:2<br/>
*
* @param content 要解析的帶有占位符的模板字符串
* @param values 按照模板占位符索引位置設(shè)置對應(yīng)的值
* @return
*/
public String resolve(String content, String... values) {
int start = content.indexOf(this.placeholderPrefix);
if (start == -1) {
return content;
}
//值索引
int valueIndex = 0;
StringBuilder result = new StringBuilder(content);
while (start != -1) {
int end = result.indexOf(this.placeholderSuffix);
String replaceContent = values[valueIndex++];
result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
}
return result.toString();
}
/**
* 解析帶有指定占位符的模板字符串,默認(rèn)占位符為前綴:${ 后綴:}<br/><br/>
* 如:template = category:${}:product:${}<br/>
* values = {"1", "2"}<br/>
* 返回 category:1:product:2<br/>
*
* @param content 要解析的帶有占位符的模板字符串
* @param values 按照模板占位符索引位置設(shè)置對應(yīng)的值
* @return
*/
public String resolve(String content, Object[] values) {
return resolve(content, Stream.of(values).map(String::valueOf).toArray(String[]::new));
}
/**
* 根據(jù)替換規(guī)則來替換指定模板中的占位符值
* @param content 要解析的字符串
* @param rule 解析規(guī)則回調(diào)
* @return
*/
public String resolveByRule(String content, Function<String, String> rule) {
int start = content.indexOf(this.placeholderPrefix);
if (start == -1) {
return content;
}
StringBuilder result = new StringBuilder(content);
while (start != -1) {
int end = result.indexOf(this.placeholderSuffix, start);
//獲取占位符屬性值,如${id}, 即獲取id
String placeholder = result.substring(start + this.placeholderPrefix.length(), end);
//替換整個占位符內(nèi)容,即將${id}值替換為替換規(guī)則回調(diào)中的內(nèi)容
String replaceContent = placeholder.trim().isEmpty() ? "" : rule.apply(placeholder);
result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
}
return result.toString();
}
/**
* 替換模板中占位符內(nèi)容,占位符的內(nèi)容即為map key對應(yīng)的值,key為占位符中的內(nèi)容。<br/><br/>
* 如:content = product:${id}:detail:${did}<br/>
* valueMap = id -> 1; pid -> 2<br/>
* 經(jīng)過解析返回 product:1:detail:2<br/>
*
* @param content 模板內(nèi)容。
* @param valueMap 值映射
* @return 替換完成后的字符串。
*/
public String resolveByMap(String content, final Map<String, Object> valueMap) {
return resolveByRule(content, placeholderValue -> String.valueOf(valueMap.get(placeholderValue)));
}
/**
* 根據(jù)properties文件替換占位符內(nèi)容
* @param content
* @param properties
* @return
*/
public String resolveByProperties(String content, final Properties properties) {
return resolveByRule(content, placeholderValue -> properties.getProperty(placeholderValue));
}
/**
* 根據(jù)對象中字段路徑(即類似js訪問對象屬性值)替換模板中的占位符 <br/><br/>
* 如 content = product:${id}:detail:${detail.id} <br/>
* obj = Product.builder().id(1).detail(Detail.builder().id(2).build()).build(); <br/>
* 經(jīng)過解析返回 product:1:detail:2 <br/>
*
* @param content 要解析的內(nèi)容
* @param obj 填充解析內(nèi)容的對象(如果是基本類型,則所有占位符替換為相同的值)
* @return
*/
public String resolveByObject(String content, final Object obj) {
if (obj instanceof Map) {
return resolveByMap(content, (Map)obj);
}
return resolveByRule(content, placeholderValue -> String.valueOf(ReflectionUtils.getValueByFieldPath(obj, placeholderValue)));
}
}
由于代碼還有一些工具類的調(diào)用因?yàn)槠鶈栴}沒有貼,如有需要可以去 https://gitee.com/objs/mayfly 該項(xiàng)目中獲取
以上這篇java解析{{}}變量名以及文本內(nèi)容替換操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Java8新特性O(shè)ptional類的詳細(xì)解讀
Optional類是一個容器類,它可以保存類型T的值,代表這個值存在?;蛘邇H僅保存null,表示這個值不存在,原來用 null 表示一個值不存在,現(xiàn)在Optional 可以更好的表達(dá)這個概念。并且可以避免空指針異常,需要的朋友可以參考下2023-05-05
SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn)代碼
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
springboot項(xiàng)目如何配置多數(shù)據(jù)源
本文介紹了如何在SpringBoot項(xiàng)目中配置多數(shù)據(jù)源,包括配置多個數(shù)據(jù)源、創(chuàng)建數(shù)據(jù)源配置類、配置事務(wù)管理器以及使用不同的Mapper,從而實(shí)現(xiàn)跨數(shù)據(jù)庫操作2025-03-03
java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實(shí)例
這篇文章主要介紹了java 用泛型參數(shù)類型構(gòu)造數(shù)組詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Java傳值調(diào)用和傳引用調(diào)用方式(參數(shù)引用為null問題)
這篇文章主要介紹了Java傳值調(diào)用和傳引用調(diào)用方式(參數(shù)引用為null問題),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Caused by: java.lang.ClassNotFoundException: org.apache.comm
這篇文章主要介紹了Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type異常,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

