import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @description
* @author: ZhiWen
* @create: 2020-04-16 11:18
**/
public class Regex {
private static Pattern p1 = Pattern.compile("^/[0-9a-zA-Z\\u4e00-\\u9fa5/_-]{2,200}$");
private static Pattern p2 = Pattern.compile("^[\\S]+_tmp_[0-9]{13}$");
private static Pattern p3 = Pattern.compile("^SELECT|select\\s\\S");
private static Pattern p4 = Pattern.compile("(\\$\\{)([\\w]+)(\\})");
public static void main(String[] args) {
regex01();
}
public static void regex01(){
HashMap<String, String> map = new HashMap<>();
map.put("uid","15");
map.put("sex","男");
map.put("age","18");
Matcher m = p4.matcher("select id,name,age,sex as sex from person where id>${uid} and sex = ${sex} order by ${sex} ${uid} group by name");
StringBuffer sb = new StringBuffer();
while (m.find()) {
//规则中${值}中的 值 一样 的数据不
String group = m.group(2);
System.out.println("符合规则中第二个的值"+group);
//下一步是替换并且把替换好的值放到sb中
System.out.println(map.get(group));
m.appendReplacement(sb, map.get(group));
System.out.println(sb.toString());
}
//把符合的数据追加到sb尾
m.appendTail(sb);
System.out.println(sb.toString());
}
}
正则匹配SQL字符串中的引用符${}
最新推荐文章于 2023-12-13 19:09:33 发布