加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

Sql正则替换

发布时间:2020-12-12 09:04:04 所属栏目:MsSql教程 来源:网络整理
导读:现在有一个小场景,数据库中某表的一个字段存储的是html代码,假如现在需要替换掉html代码中的所有标签。 我们当然可以在C#中这样做: regex = ( [^ ); cleanedHtml = regex.Replace(html, ); 可是我并不想再写个循环去遍历每条记录,然后保存每条记录,我想

现在有一个小场景,数据库中某表的一个字段存储的是html代码,假如现在需要替换掉html代码中的所有标签。

我们当然可以在C#中这样做:

regex = ([^<]*");
cleanedHtml = regex.Replace(html,);

可是我并不想再写个循环去遍历每条记录,然后保存每条记录,我想在数据库中一步到位,而sql只提供了简单的replace函数,这个函数明显不能达到咱的要求,那就去写一个自定义函数吧。

函数源代码如下:

dboregexReplace 
@source @regexp 1000@replace 1000@globalReplace 1@ignoreCase 0 1000@hr @objRegExp @result 5000<span style="color: blue;">exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OACreate <span style="color: red;">'VBScript.RegExp'<span style="color: gray;">,@objRegExp <span style="color: blue;">OUTPUT
IF
@hr <span style="color: gray;"><>
0 <span style="color: blue;">begin
exec
@hr <span style="color: gray;">=
<span style="color: maroon;">sp_OADestroy
@objRegExp
<span style="color: blue;">return
<span style="color: gray;">null
<span style="color: blue;">end
exec
@hr <span style="color: gray;">=
<span style="color: maroon;">sp_OASetProperty
@objRegExp<span style="color: gray;">,
<span style="color: red;">'Pattern'
<span style="color: gray;">,
@regexp
<span style="color: blue;">IF
@hr <span style="color: gray;"><>
0 <span style="color: blue;">begin
exec
@hr <span style="color: gray;">=
<span style="color: maroon;">sp_OADestroy
@objRegExp
<span style="color: blue;">return
<span style="color: gray;">null
<span style="color: blue;">end
exec
@hr <span style="color: gray;">=
<span style="color: maroon;">sp_OASetProperty
@objRegExp<span style="color: gray;">,
<span style="color: red;">'Global'
<span style="color: gray;">,@globalReplace
<span style="color: blue;">IF @hr <span style="color: gray;"><> 0 <span style="color: blue;">begin
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OADestroy @objRegExp
<span style="color: blue;">return <span style="color: gray;">null
<span style="color: blue;">end
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OASetProperty @objRegExp<span style="color: gray;">,<span style="color: red;">'IgnoreCase'<span style="color: gray;">,@ignoreCase
<span style="color: blue;">IF @hr <span style="color: gray;"><> 0 <span style="color: blue;">begin
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OADestroy @objRegExp
<span style="color: blue;">return <span style="color: gray;">null
<span style="color: blue;">end
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OAMethod @objRegExp<span style="color: gray;">,<span style="color: red;">'Replace'<span style="color: gray;">,@result <span style="color: blue;">OUTPUT<span style="color: gray;">,@source<span style="color: gray;">,@replace
<span style="color: blue;">IF @hr <span style="color: gray;"><> 0 <span style="color: blue;">begin
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OADestroy @objRegExp
<span style="color: blue;">return <span style="color: gray;">null
<span style="color: blue;">end
exec @hr <span style="color: gray;">= <span style="color: maroon;">sp_OADestroy @objRegExp
<span style="color: blue;">IF @hr <span style="color: gray;"><> 0 <span style="color: blue;">begin
return <span style="color: gray;">null
<span style="color: blue;">end

return @result
<span style="color: blue;">end

需要注意的是,即使写好了这个函数,也并不能马上使用。执行这个函数时可能会出现以下的错误:

这是因为未开启Ole Automation Procedures选项,。执行下面的语句开启这个选项:

11

所有的准备工作都已经做好,那就试验一下吧。

dboregexReplace[^<]*'11

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.

调用代码:

dboregexReplacehtml(.|n)*?'1

dboregexReplaceKey Contact:
Mr Jack,Zhou
General Manager

Mr Adu,Ho
Marketing Director
Overseas Sales

Ms Winny,Luo
Sales Manager
Overseas Sales

' ]*>'0

Books 
[Description] dboregexReplace[Description]1

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读