根據短信內容模糊匹配短信模板,代碼如下
public static void main(String[] args) {
String regex = "#[\\d\\D]+#";//正則匹配模板中變量
String template1 = "您的手機驗證碼是#code#。本條信息無需回覆";//模板1
String regEx1 = "^" + template1.replaceAll(regex, "[\\\\d\\\\D]+") + "$";//根據模板生成正則
// 使用正則匹配
String s1 = "您的手機驗證碼是323345。本條信息無需回覆";
boolean flag1 = Pattern.compile(regEx1).matcher(s1).find();
System.out.println(flag1);
String template2 = "歡迎使用#app#,您的手機驗證碼是#code#。本條信息無需回覆";//模板2
String regEx2 = "^" + template2.replaceAll(regex, "[\\\\d\\\\D]+") + "$";//根據模板生成正則
// 使用正則匹配
String s2 = "歡迎使用石鳴捷,您的手機驗證碼是323345。本條信息無需回覆";
boolean flag2 = Pattern.compile(regEx2).matcher(s2).find();
System.out.println(flag2);
}