java正则匹配空格字符串正则表达式截取字符串
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 = Patternpile(patternStr);
String[] dataArr = pattern.split(input);
Set<String> set = new TreeSet<String>();
for (String str : dataArr) {
System.out.println(str);
if (str.startsWith("@")) {
str = placeAll("@", "");
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 = Patternpile(regex);
Matcher matcher = pattern.matcher(input);
Set<String> set = new TreeSet<String>();
while(matcher.find()){
set.up());
}
System.out.println(set);
}
}
2014年11⽉26⽇ 16:50:31
2015年 2⽉10⽇ 23:43:45
落⾬
394263788
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论