Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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
用java正则表达式将占位符替换为hashmap数据_Java_Regex - Fatal编程技术网

用java正则表达式将占位符替换为hashmap数据

用java正则表达式将占位符替换为hashmap数据,java,regex,Java,Regex,我有一个带有占位符的模板 Dear [[user.firstname]] [[user.lastname]] Message [[other.msg]] 我在地图上收集了一些数据 Map data = new HashMap(); data.put("user.firstname","John"); data.put("user.lastname","Kannan"); data.put("other.msg","Message goes here..."); 我想创建一个J

我有一个带有占位符的模板

Dear [[user.firstname]] [[user.lastname]] 

Message [[other.msg]]
我在地图上收集了一些数据

Map data = new HashMap();  
data.put("user.firstname","John");
data.put("user.lastname","Kannan");    
data.put("other.msg","Message goes here...");

我想创建一个Java正则表达式,用模板上的相关占位符(在
[[]]
中)替换地图数据值。

那么,问题出在哪里

String text = "Dear [[user.firstname]] [[user.lastname]]";
for (String key : data.keySet()) {
    String pattern = key.replace("[", "\\[").replace("]", "\\");
    text = text.replaceAll(pattern, data.get(key));
}

小心。可能您必须复制它们,即写入\\而不是\。只需调试代码几分钟,解决方案就准备好了。

在Java中,单用正则表达式无法做到这一点,您需要用一些逻辑来包装它

这里有一种方法可以为您做到这一点:

public static String replaceValues(final String template,
    final Map<String, String> values){

    final StringBuffer sb = new StringBuffer();
    final Pattern pattern =
        Pattern.compile("\\[\\[(.*?)\\]\\]", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(template);
    while(matcher.find()){
        final String key = matcher.group(1);
        final String replacement = values.get(key);
        if(replacement == null){
            throw new IllegalArgumentException(
               "Template contains unmapped key: "
                + key);
        }
        matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    return sb.toString();

}
公共静态字符串替换值(最终字符串模板,
最终地图值){
最终StringBuffer sb=新StringBuffer();
最终图案=
Pattern.compile(“\\[\\[(.*?\\]\\\]”,Pattern.DOTALL);
最终匹配器匹配器=pattern.Matcher(模板);
while(matcher.find()){
最终字符串键=matcher.group(1);
最终字符串替换=values.get(键);
如果(替换==null){
抛出新的IllegalArgumentException(
“模板包含未映射的密钥:”
+键);
}
匹配器更换(sb,更换);
}
(某人);
使某人返回字符串();
}

为什么需要一个正则表达式?为什么不干脆做:

for (Map.Entry<String, String> replacement : data.entrySet()) {
    s = s.replace("[[" + replacement.getKey() + "]]", replacement.getValue());
}
for(Map.Entry替换:data.entrySet()){
s=s.replace(“[[”+replacement.getKey()+“]]”,replacement.getValue());
}

我建议使用
附录替换方法:

Map<String, String> data = new HashMap<String, String>();
...
Pattern p = Pattern.compile("\\[\\[([a-zA-Z.]+)\\]\\]");
Matcher m = p.matcher("Dear [[user.firstname]] [[user.lastname]]");
StringBuffer sb = new StringBuffer();
while (m.find()) {
    String reg1 = m.group(1);
    String value = data.get(reg1);
    m.appendReplacement(sb, Matcher.quoteReplacement(value == null ? "" : value));
}
m.appendTail(sb);
Map data=newhashmap();
...
Pattern p=Pattern.compile(“\\[\\[([a-zA-Z.]+)\\]\\]\\]”);
Matcher m=p.Matcher(“亲爱的[[user.firstname]][[user.lastname]]”;
StringBuffer sb=新的StringBuffer();
while(m.find()){
字符串reg1=m.group(1);
字符串值=data.get(reg1);
m、 appendReplacement(sb,Matcher.quoteReplacement(value==null?”:value));
}
m、 (某人);

您切换了字符串。使用
Pattern.compile(“\\[\\([a-zA-Z.]+)\]\\]\\]\]”
p.matcher(“亲爱的[[user.firstname]]][[user.lastname]]”
@Sean Patrick Floyd,感谢您的更正,我应该在发布之前测试代码。实际上,我把这个片段绑定到vim中的一个键盘快捷键上,所以我只需填写模式、字符串和替换项。@Mr.polywhill,因为没有:-)假设你的地图有100万个键,你必须转换10亿个字符串。这不能很好地扩展性能是问题所在。例如,如果我的字典中有兆字节的文本和数千个占位符呢?