java正規表示式替換

livedba發表於2012-06-04
java正規表示式替換[@more@]//正規表示式替換--尾部為空格或者;
String regex = "#w+(()|(;)|()))";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(sql);
StringBuffer sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()-1));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()-1).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ");
}
matcher.appendTail(sbf);

sql = sbf.toString();
//正規表示式替換--尾部是字串結尾
regex = "#w+($)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ");
}
matcher.appendTail(sbf);
sql=sbf.toString();

//正規表示式替換--尾部是括號的
regex = "#w+())";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(sql);
sbf = new StringBuffer();
while(matcher.find()){
//System.out.println(sql.substring(matcher.start()+1, matcher.end()));
//System.out.println(sql.substring(matcher.start()+1, matcher.end()).trim());
String argsValue = jbpmArgs.get(sql.substring(matcher.start()+1, matcher.end()-1).trim());
matcher.appendReplacement(sbf,argsValue);
sbf.append(" ) ");
}
matcher.appendTail(sbf);
sql=sbf.toString();

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25261409/viewspace-1058415/,如需轉載,請註明出處,否則將追究法律責任。

相關文章