package test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test1 {
public static void main(String[] args) {
String hql = " from Users abc by type order by id abc limit 0,3";
removeOrders(hql);
}
/**
* 使用Matcher.appendReplacement(String str1, String str2)时要注意,最好这样用:
Matcher.appendReplacement(String str1, String str2.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\$", "\\\\\\$"));
1replaceAll("\\\\", "\\\\\\\\")是为了把"\"符号加上,如果不进行这部处理,"\"符号就会被删除!
2replaceAll("\\$", "\\\\\\$")是为了把"$"符号转义,因为"$"后加数字是表示第几组的意思!
* @param hql
* @return
*/
private static String removeOrders(String hql){
// Assert.hasText(hql);
Pattern p=Pattern.compile("abc",Pattern.CASE_INSENSITIVE);
Matcher m=p.matcher(hql);
StringBuffer sb=new StringBuffer();
//find()方法表示是否有符合的字符串,group()方法则可以将符合的字符串返回
while(m.find()){
System.out.println(m.group());
//将当前匹配子串替换为指定字符串,并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个StringBuffer对象里。
//appendReplacement方法先添加字符串中从当前位置到下一个 匹配位置之间的所有字符,然后添加替换值。
m.appendReplacement(sb,"ttitfly");
System.out.println(sb.toString());
}
//将最后一次匹配工作后剩余的字符串添加到一个StringBuffer对象里。
//appendTail添加的 是字符串中从最后一次匹配的位置之后开始,直到结尾的部分
m.appendTail(sb);
System.out.println(sb.toString());
return sb.toString();
}
}
输出结果:
abc
from Users ttitfly
abc
from Users ttitfly by type order by id ttitfly
from Users ttitfly by type order by id ttitfly limit 0,3
分享到:
相关推荐
Draw Pattern Matches Position
org.dom4j.rule A Pattern based XML rule engine which implements the full XSLT processing model while allowing any Action to be fired if a pattern matches. org.dom4j.rule.pattern org.dom4j.swing A ...
3.19 Dispatching Based on Pattern Matches 3.20 Evaluating Code Inside Strings 3.21 Replacing Python Code with the Results of Executing That Code 3.22 Module: Yet Another Python Templating ...
16.2 Exact solution: a plane wave interference pattern matches the waveguide boundary conditions 196 16.3 Waveguide vs. coax for low-loss power transmission 201 16.4 Waveguide impedance 201 16.5 ...
主要介绍了Java matches类,Pattern类及matcher类用法,结合实例形式分析了java matches类,Pattern类及matcher类针对字符串常见操作技巧与相关注意事项,需要的朋友可以参考下
matches() :尝试将整个区域与模式进行匹配。 find() :尝试找到匹配模式的输入序列的下一个子序列。 lookingAt() :尝试将输入序列从区域开头开始与模式相匹配。 这三个函数都是Matcher类中的方法,大部分时候配合...
Java中的正则表达式是一种强大的文本匹配工具,它可以用来检查和操作字符串。...Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); boolean isMatch = matcher.fin
if(matcher.matches()){ for(int i=0;i()/2;i++){ if(str.replaceAll(str.substring(0,i+1), "").length()==0){ return false; } } return true; } return false; }
* 匹配email * * @param strEmail ... Pattern pattern = Pattern .compile("^([\\u4E00-\\uFA29]|[\\uE7C7-\\uE7F3]|[\\w])*$"); Matcher matcher = pattern.matcher(user); return matcher.find(); }
DIRegEx is a library of Delphi (Embarcadero / CodeGear / Borland) components and procedures that implement regular expression pattern matching using the same syntax and semantics as Perl 5.10, with ...
regEx.Pattern = "002[\d]{3}|000[\d]{3}|300[\d]{3}|600[\d]{3}|60[\d]{4}" ' 设置模式。 regEx.IgnoreCase = true ' 设置是否区分字符大小写。 regEx.Global = True ' 设置全局可用性。 Matches = regEx.test...
DIRegEx is a library of Delphi (Embarcadero / CodeGear / Borland) components and procedures that implement regular expression pattern matching using the same syntax and semantics as Perl 5.10, with ...
DIRegEx is a library of Delphi (Embarcadero / CodeGear / Borland) components and procedures that implement regular expression pattern matching using the same syntax and semantics as Perl 5.10, with ...
收录了许多 前辈们的 私藏方法,如: /** * 判断... return pattern.matcher(str).matches(); } <br> /** * 人民币转成大写 * * @param value * @return String */ …………
举个例子如果字符串为”a123″,正则表达式为”\w\d\d\d”,则matches()方法才返回true,换言之,就是要匹配的字符串需要跟正则表达式一一对应,字母对应字母,数字对应数字,所以称之为完全匹配,错一位都不行。...
Use pattern as the pattern; useful to protect patterns beginning with a -. -f, --file=FILE Obtain patterns from file, one per line. The empty file contains zero patterns, and therefore matches ...
1. preg_match — 执行一个正则表达式匹配int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )搜索subject与pattern给定的正则表达式的一个匹配....
preg_match ( $pattern , $subject , $matches ) 搜索subject与pattern给定的正则表达式的一个匹配. 参数 : pattern : 要搜索的模式,字符串类型(正则表达式)。 subject : 输入的字符串。 matches :(可有可无)...
上面的Pattern对象可以重复使用多次,如果某个正则表达式仅需一次使用,可直接使用Pattern类的静态matches方法,此方法自动把指定字符串编译成匿名的Pattern对象,并执行匹配。 Boolean b=Pattern.matches(“a*b”,...
The LP Viewer is used to quickly search and locate the correct Land Pattern that matches your component dimensions or manufacturer’s case code information or your personal corporate part number.