Regular use of simple java

It contains the string to find whether the query contains a fragment # {name} true return There comprising

String context = "select * from t_user where (name = #{name} or username = #{name}) and age > #{age}";
String regex = ".*\\#\\{name\\}.*";
boolean is = Pattern.matches(regex,context);

All Match # {} Nothing

String context = "select * from t_user where (name = #{name} or username = #{name}) and age > #{age}";
//String regex = "\\{([^}]*)\\}";
String regex = "\\#\\{(.*?)\\}";
//创建 Pattern 对象
Pattern r = Pattern.compile(regex);
//创建 Matcher 对象
Matcher m = r.matcher(context);
while (m.find()){
     System.out.println(m.group() + "=" + m.group(1));
}

Output content

#{name}=name
#{name}=name
#{age}=age

 

Guess you like

Origin www.cnblogs.com/rchao/p/11011528.html