Sql正則替換


現在有一個小場景,數據庫中某表的一個字段存儲的是html代碼,假如現在需要替換掉html代碼中的所有<a>標簽。

我們當然可以在C#中這樣做:

Regex regex = new Regex(@"<a[^>]*>[^<]*</a>");
string cleanedHtml = regex.Replace(html, "");

可是我並不想再寫個循環去遍歷每條記錄,然后保存每條記錄,我想在數據庫中一步到位,而sql只提供了簡單的replace函數,這個函數明顯不能達到咱的要求,那就去寫一個自定義函數吧。

函數源代碼如下:

--SQL正則替換函數
CREATE function dbo.regexReplace 
( 
@source ntext, --原字符串
@regexp varchar(1000), --正則表達式
@replace varchar(1000), --替換值
@globalReplace bit = 1, --是否是全局替換
@ignoreCase bit = 0 --是否忽略大小寫
) 
returnS varchar(1000) AS 
begin 
declare @hr integer 
declare @objRegExp integer 
declare @result varchar(5000) 

exec @hr = sp_OACreate 'VBScript.RegExp', @objRegExp OUTPUT 
IF @hr <> 0 begin 
exec @hr = sp_OADestroy @objRegExp 
return null 
end 
exec @hr = sp_OASetProperty @objRegExp, 'Pattern', @regexp 
IF @hr <> 0 begin 
exec @hr = sp_OADestroy @objRegExp 
return null 
end 
exec @hr = sp_OASetProperty @objRegExp, 'Global', @globalReplace 
IF @hr <> 0 begin 
exec @hr = sp_OADestroy @objRegExp 
return null 
end 
exec @hr = sp_OASetProperty @objRegExp, 'IgnoreCase', @ignoreCase 
IF @hr <> 0 begin 
exec @hr = sp_OADestroy @objRegExp 
return null 
end 
exec @hr = sp_OAMethod @objRegExp, 'Replace', @result OUTPUT, @source, @replace 
IF @hr <> 0 begin 
exec @hr = sp_OADestroy @objRegExp 
return null 
end 
exec @hr = sp_OADestroy @objRegExp 
IF @hr <> 0 begin 
return null 
end 

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;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

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

Example1:忽略大小寫並替換

select dbo.regexReplace('<A HREF="www.jileiba.com" target="_blank" style="color:red;">123456</a>','<a[^>]*>[^<]*</a>','',1,1)

Example2: 使用貪婪匹配

html代碼:

<p>
    Also Available - <a style="text-decoration: none" href="/isbn/9780199218691"><font
        color="#000FF"><b>Smith & Hogan: Criminal Law Cases & Materials 10th ed</b></font></a>
    <p>
        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.</p>
</p>

調用代碼:

select dbo.regexReplace(html,'<a[^>]*>(.|\n)*?</a>','',1,1)

Example3:去除html標簽

select dbo.regexReplace('<p><b>Key Contact:</b><br> Mr  Jack,  Zhou<br> General Manager<br> <p> Mr  Adu,  Ho<br> Marketing Director<br> Overseas Sales<br> <p> Ms  Winny,  Luo<br> Sales Manager<br> Overseas Sales<br> <p>'
,'<[^>]*>','',1,0) 

Example4:數據庫字段值替換

update Books 
set [Description] = dbo.regexReplace([Description],'<a[^>]*>(.|\n)*?</a>','',1,1)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM