當前位置:編程學習大全網 - 源碼下載 - java過濾sql關鍵字的正則替換掉

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 procedure 'sys.sp_OACreate' of component 'Ole Automation Procedures' 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 Procedures' by using sp_configure. For more information about enabling 'Ole Automation Procedures', see "Surface Area Configuration" in SQL Server Books Online.

這是因為未開啟Ole Automation Procedures選項,MSDN中的Ole Automation Procedures選項。執行下面的語句開啟這個選項:sp_configure'show advanced options',1;GO

RECONFIGURE;GOsp_configure'Ole Automation Procedures',1;GO

RECONFIGURE;GO

所有的準備工作都已經做好,那就試驗壹下吧。

Example1:忽略大小寫並替換selectdbo.regexReplace('123456',']*>[^','',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,']*>(.|\n)*?','',1,1)

Example3:去除html標簽selectdbo.regexReplace('

Key Contact:

Mr Jack, Zhou

General Manager

Mr Adu, Ho

Marketing Director

Overseas Sales

MsWinny, Luo

Sales Manager

Overseas Sales',']*>','',1,0)

Example4:數據庫字段值替換updateBooks。

  • 上一篇:用booth算法求[x*y]補。x=0.1101,y=-0.1010
  • 下一篇:小程序開發需要什麽技術
  • copyright 2024編程學習大全網