java表達式sql
❶ java 正則表達式是什麼
不同情況下的正則表達式:
匹配首尾空格的正則表達式:(^s*)|(s*$)。
匹配html標簽的正則表達式:<(.*)>(.*)</(.*)>|<(.*)/>。
配空行的正則表達式: [s| ]* 。
整數或者小數:^[0-9]+.{0,1}[0-9]{0,2}$。
只能輸入數字:"^[0-9]*$"。
只能輸入n位的數字:"^d{n}$"。
只能輸入至少n位的數字:"^d{n,}$"。
只能輸入m~n位的數字:。"^d{m,n}$"
只能輸入零和非零開頭的數字:"^(0|[1-9][0-9]*)$"。
只能輸入有兩位小數的正實數:"^[0-9]+(.[0-9]{2})?$"。
只能輸入有1~3位小數的正實數:"^[0-9]+(.[0-9]{1,3})?$"。
只能輸入非零的正整數:"^+?[1-9][0-9]*$"。
只能輸入非零的負整數:"^-[1-9][]0-9"*$。
只能輸入長度為3的字元:"^.{3}$"。
只能輸入由26個英文字母組成的字元串:"^[A-Za-z]+$"。
只能輸入由26個大寫英文字母組成的字元串:"^[A-Z]+$"。
只能輸入由26個小寫英文字母組成的字元串:"^[a-z]+$"。
只能輸入由數字和26個英文字母組成的字元串:"^[A-Za-z0-9]+$"。
只能輸入由數字、26個英文字母或者下劃線組成的字元串:"^w+$"。
驗證用戶密碼:"^[a-zA-Z]w{5,17}$"正確格式為:以字母開頭,長度在6~18之間,只能包含字元、數字和下劃線。
驗證是否含有^%&',;=?$"等字元:"[^%&',;=?$x22]+"。
只能輸入漢字:"^[u4e00-u9fa5]{0,}$"。
驗證Email地址:"^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$"。
驗證一年的12個月:"^(0?[1-9]|1[0-2])$"正確格式為:"01"~"09"和"1"~"12"。
驗證一個月的31天:"^((0?[1-9])|((1|2)[0-9])|30|31)$"正確格式為;"01"~"09"和"1"~"31"。
匹配中文字元的正則表達式: [u4e00-u9fa5]。
匹配雙位元組字元(包括漢字在內):[^x00-xff]。
應用:計算字元串的長度(一個雙位元組字元長度計2,ASCII字元計1)String.prototype.len=function(){returnthis.replace(/[^x00-xff]/g,"aa").length;}。
❷ java過濾sql關鍵字的正則替換掉
java過濾sql關鍵字的正則替換掉方法如下:
可以在C#中這樣做:Regexregex = newRegex(@"]*>[^");
stringcleanedHtml = regex.Replace(html, "");
可是我並不想再寫個循環去遍歷每條記錄,然後保存每條記錄,我想在資料庫中一步到位,而sql只提供了簡單的replace函數,這個函數明顯不能達到咱的要求,那就去寫一個自定義函數吧。
函數源代碼如下:CREATE functiondbo.regexReplace
(@source ntext,--原字元串@regexp varchar(1000),--正則表達式@replace varchar(1000),--替換值@globalReplace bit=1,--是否是全局替換@ignoreCase bit=0 --是否忽略大小寫)returnS varchar(1000)AS
begin
declare@hr intege
declare@objRegExp integer
declare@result varchar(5000)exec@hr =sp_OACreate'VBScript.RegExp',@objRegExp OUTPUT
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Pattern',@regexp
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Global',@globalReplace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'IgnoreCase',@ignoreCase
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OAMethod@objRegExp,'Replace',@result OUTPUT,@source,@replace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OADestroy@objRegExp
IF@hr <>0 begin
returnnullend
return@result
end
需要注意的是,即使寫好了這個函數,也並不能馬上使用。執行這個函數時可能會出現以下的錯誤:Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to procere 'sys.sp_OACreate' of component 'Ole Automation Proceres' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Proceres' by using sp_configure. For more information about enabling 'Ole Automation Proceres', see "Surface Area Configuration" in SQL Server Books Online.
這是因為未開啟Ole Automation Proceres選項,MSDN中的Ole Automation Proceres選項。執行下面的語句開啟這個選項:sp_configure'show advanced options',1;GO
RECONFIGURE;GOsp_configure'Ole Automation Proceres',1;GO
RECONFIGURE;GO
所有的准備工作都已經做好,那就試驗一下吧。
Example1:忽略大小寫並替換selectdbo.regexReplace(',']*>[^','',1,1)
Example2: 使用貪婪匹配
html代碼:
Also Available - Smith & Hogan: Criminal Law Cases & Materials 10th ed
There is, as ever, detailed analysis of the many recent case developments, in particular,
a revision of the chapter dealing with secondary liability and joint enterprise.
調用代碼:selectdbo.regexReplace(html,']*>(.|
)*?','',1,1)
Example3:去除html標簽selectdbo.regexReplace('
Key Contact:
Mr Jack, Zhou
General Manager
Mr A, Ho
Marketing Director
Overseas Sales
MsWinny, Luo
Sales Manager
Overseas Sales',']*>','',1,0)
Example4:資料庫欄位值替換updateBooks。
❸ java 驗證字元串是否為sql語句 並且是否包含 select 關鍵字
java驗證字元串是否為sql語句,是否包含select關鍵字,主要使用的是正則表達式來進行驗證,如下:
importjava.util.*;
importjava.text.*;
classsqltest
{
publicstaticvoidmain(String[]args)
{
Stringspan="selectaaaa.idname,hello,typet,hfromdatasaaaa,citybwherea.id=b.idandclike'e%'andnameisnull";
span=span.toUpperCase();//測試用sql語句
System.out.println(span);
Stringcolumn="(\w+\s*(\w+\s*){0,1})";//一列的正則表達式匹配如proctp
Stringcolumns=column+"(,\s*"+column+")*";//多列正則表達式匹配如proctp,categoryc,warehousew
Stringownerenable="((\w+\.){0,1}\w+\s*(\w+\s*){0,1})";//一列的正則表達式匹配如a.proctp
Stringownerenables=ownerenable+"(,\s*"+ownerenable+")*";//多列正則表達式匹配如a.proctp,a.categoryc,b.warehousew
Stringfrom="FROM\s+"+columns;
Stringcondition="(\w+\.){0,1}\w+\s*(=|LIKE|IS)\s*'?(\w+\.){0,1}[\w%]+'?";//條件的正則表達式匹配如a=b或aisb..
Stringconditions=condition+"(\s+(AND|OR)\s*"+condition+"\s*)*";//多個條件匹配如a=bandclike'r%'ordisnull
Stringwhere="(WHERE\s+"+conditions+"){0,1}";
Stringpattern="SELECT\s+(\*|"+ownerenables+"\s+"+from+")\s+"+where+"\s*";//匹配最終sql的正則表達式
System.out.println(pattern);//輸出正則表達式
System.out.println(span.matches(pattern));//是否比配
}
}
❹ java 怎麼用正則表達式解析sql中的表名,已有半成品,求改善
Stringstr="from\s+(.*)\s+where?";
Stringsql="select*fromtable,table2wherea=b";
Patternp=Pattern.compile(str);
Matchermatcher=p.matcher(sql);
while(matcher.find()){
Stringstring=matcher.group(1);
System.out.println(string);
}