码迷,mamicode.com
首页 > 编程语言 > 详细

java 正则简单使用

时间:2019-06-12 19:37:42      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:and   find   java   select   string   pattern   system   bool   compile   

查找是否包含字串 查询是否包含 #{name} 片段 这里有包含所以返回true

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

匹配所有    #{任何内容}

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));
}

输出内容为

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

 

java 正则简单使用

标签:and   find   java   select   string   pattern   system   bool   compile   

原文地址:https://www.cnblogs.com/rchao/p/11011528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!