java占位符

String str="我是{0},我来自{1},今年{2}岁,{3}";
   String[] arr={"中国人","北京","22","谢谢"};
   Matcher m=Pattern.compile("\\{(\\d)\\}").matcher(str);
        while(m.find()){
            str=str.replace(m.group(),arr[Integer.parseInt(m.group(1))]);
        }
        System.out.println(str);



Map map=new HashMap();
map.put("msgSource", "N");
map.put("title", "标题");
map.put("cnt", "内容");
String str="[title],您有一条新消息,[cnt]11";
  
        String regex = "\\[(\\w+)\\]";   
        Matcher m=Pattern.compile(regex).matcher(str);
        while(m.find()){
        if(map.get(m.group().substring(1, m.group().length()-1))!=null){
        str=str.replace(m.group(),map.get(m.group().substring(1, m.group().length()-1)));
        }
        else{
        str=str.replace(m.group(),"");
        }
        }
        System.out.println(str);