/**
* @describe java中字符串参数化符号${}的正则解析
* @author cxy
* @date 2019年10月10日16:23:41
*
*/

public class RegExp {

public boolean match(String reg, String str) {
return Pattern.matches(reg, str);
}

public List<String> find(String reg, String str) {
Matcher matcher = Pattern.compile(reg).matcher(str);
List<String> list = new ArrayList<String>();
while (matcher.find()) {
list.add(matcher.group());
}
return list;
}

public List<String> find(String reg, String str, int index) {
Matcher matcher = Pattern.compile(reg).matcher(str);
List<String> list = new ArrayList<String>();
while (matcher.find()) {
list.add(matcher.group(index));
}
return list;
}

public String findString(String reg, String str, int index) {
String returnStr = null;
List<String> list = this.find(reg, str, index);
if (list.size() != 0){
returnStr = list.get(0);
}
return returnStr;
}

public String findString(String reg, String str) {
String returnStr = null;
List<String> list = this.find(reg, str);
if (list.size() != 0){
returnStr = list.get(0);
}
return returnStr;
}

public List<String> getKeywords(String p){
String reg = "(?<=(?<!\\\\)\\$\\{)(.*?)(?=(?<!\\\\)\\})";
RegExp re = new RegExp();
List<String> list = re.find(reg, p);
return list;
}
}

String smsContent = "这是${phone}我的${busi_type}一批${business_hall}测试数据${duration}2019年10月11日14:47:06";

RegExp re = new RegExp();
List<String> list = re.getKeywords(smsContent);

//后台查询

List<Object> spList = this.baseSmsService.selectParamsByTypeAndCode(vo);
Map<String,String> map = new HashMap<String,String>();
ArrayList<String> arr = new ArrayList<String>();
for(int i=0;i<spList.size();i++){//遍历查询到的结果集,并将paramKey封装进一个ArrayList中
map = (Map<String,String>)spList.get(i);
arr.add(map.get("PARAMKEY"));
}

//匹配用户输入的动态参数是否存在——遍历一个一个判断
for(int i=0;i<list.size();i++){
  System.out.println(list.get(i) + "是否已经存在" + arr.contains(list.get(i)));
}

 

//匹配用户输入的动态参数是否存在——全量匹配,只有list中的全部元素都存在于arr中,才返回true

System.out.println(arr .containsAll(list));