首页 > 编程语言 > 详细

java 正则简单使用

时间:2019-06-12 19:08:27      阅读:109      评论:0      收藏:0      [点我收藏+]

查找是否包含字串 查询是否包含 #{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 正则简单使用

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!