java正则匹配空格字符串正则表达式剪切字符串
要求:从一堆sql中检索特定字符串。
例如,' company _ code=@ ccandproject _ id=@ PID; update t set a=@aa,b=@bb,c=@cd,ttt=@ttt; update t2 set d=@bb ';
要将所有以@开头的内容作为变量取出并加重,请玩以下游戏。
ps :其中,正则匹配空格为“s”
public class test { publicstaticvoidmain (string [ ] args ) {//string input=' update t set a=@ aa,b=@bb,c=@cd,tttttt
string input=' company _ code=@ ccand project _ id=@ PID; update t set a=@aa,b=@bb,c=@cd,ttt=@ttt; update t2 set d=@bb ';
stringPatternstr='(s*=s* )|(s*,s* )|)s*; s*(|(s ) );
pattern pattern=pattern.com pile (patternstr;
string [ ] data arr=pattern.split (input;
Set set=new TreeSet (; for (stringstr 3360数据阵列) {
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 { publicstaticvoidmain (string [ ] args ) {//string input=' update t set a=@ aa,b=@bb,c=@cd,TTT
string input=' company _ code=$ CSC 223 @ ccand 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.com pile (regex );
matcher matcher=pattern.matcher (input );
Set set=new TreeSet (; wile(Matcher.find ) () ) ) ) )。
set.add(Matcher.group ) );
}
system.out.println(set );
}
}
2014年11月26日16:50:31
2015年2月10日23:43:45
要下雨了
394263788