package cn.autoform.web.client.util.auto; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import cn.autoform.util.tool.JSONTool; public class OnlyCharacters { private static final Pattern RULE_REGEXT = Pattern.compile("[$][a-zA-Z]+[{][^}]+[}]"); public static String getOnlyStr(String rule){ RuleInfo info = getRuleInfo(rule); for(Map.Entry entry : info.getParam().entrySet()){ info.setRule(info.getRule().replace(entry.getKey(), entry.getValue().getVal())); } return info.getRule(); } // public static void main(String[] args) { // System.err.println(getOnlyStr("$d{YYYY-MM}_$rs{9}_$rn{8}")); // } public static RuleInfo getRuleInfo(String rule) { Map param = new HashMap(); Matcher matcher = RULE_REGEXT.matcher(rule); String group = null, key = null; int index = 0; while(matcher.find()){ group = matcher.group(); key = String.format("#%d#", index); rule = rule.substring(0, rule.indexOf(group)) + key + rule.substring(rule.indexOf(group)+group.length(), rule.length()); param.put(key, new RuleParam(group)); index ++; } return new RuleInfo(rule, param); } /** * 公式信息 * @author WangYX * */ public static class RuleInfo{ private String rule; private Map param; public RuleInfo(String rule, Map param) { super(); this.rule = rule; this.param = param; } public String getRule() { return rule; } public void setRule(String rule) { this.rule = rule; } public Map getParam() { return param; } public void setParam(Map param) { this.param = param; } } }