java 正则简单使用

Posted rchao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 正则简单使用相关的知识,希望对你有一定的参考价值。

查找是否包含字串 查询是否包含 #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 正则简单使用的主要内容,如果未能解决你的问题,请参考以下文章