java 正则匹配空格字符串 正则表达式截取字符串
需求:从一堆sql中取出某些特定字符串:
比如配置的sql语句为:"company_code = @cc and project_id = @pid ; update t set a = @aa,b=@bb,c=@cd,ttt=@ttt;update t2 set d=@bb";
我要拿出所有的以@开头的作为变量,并且去重,则如下玩:
ps:其中正则匹配空格是 “\s+”
public class Test { public static void main(String[] args) { //String input = "update t set a = @aa,b=@bb,c=@cd,ttt=@ttt;update t2 set d=@bb"; String input = "company_code = @cc and project_id = @pid ; update t set a = @aa,b=@bb,c=@cd,ttt=@ttt;update t2 set d=@bb"; String patternStr = "(\\s*=\\s*)|(\\s*,\\s*)|(\\s*;\\s*)|(\\s+)"; Pattern pattern = Pattern.compile(patternStr); String[] dataArr = pattern.split(input); Setset = new TreeSet (); for (String str : dataArr) { System.out.println(str); if (str.startsWith("@")) { str = str.replaceAll("@", ""); set.add(str); } } System.out.println(set); }
[aa, bb, cc, cd, pid, ttt]输出如下:
截图如下:
后来又优化了一下,既然是找出以@开头的,那么再换一个正则:
public class Tst { public static void main(String[] args) { //String input = "update t set a = @aa,b=@bb,c=@cd,ttt=@ttt;update t2 set d=@bb"; String input = "company_code = $csc223@cc and project_id = @pid ; update t set a = @aa,b=@cd,c=@cd,ttt=@ttt;update t2 set d=@bb"; String regex = "@\\w+\\s?"; //regex = "(\\s*=\\s*)|(\\s*,\\s*)|(\\s*;\\s*)|(\\s+)"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); Setset = new TreeSet (); while(matcher.find()){ set.add(matcher.group()); } System.out.println(set); }}
2014年11月26日 16:50:31
2015年 2月10日 23:43:45
落雨
394263788