---
title: "数据库字段的正则批量替换例子"
author: "csstome"
date: "2008-12-26T14:20:00Z"
tags: ["C", "C#", "C++", "Microsoft", "正则表达式"]
slug: "untitled"
draft: false
lastmod: "2008-12-26T14:20:00Z"
id: 1033054
url: "https://csstome.iteye.com/blog/1033054"
---

'123&lt;和这个&gt;56&lt;7&gt;8'替换为'123568'  
最直接的方法就是想到正则表达式  
如果是SQLServer则不直接支持，可以借助js的com组件

&lt;meta content="Word.Document" name="ProgId"&gt; &lt;meta content="Microsoft Word 12" name="Generator"&gt; &lt;meta content="Microsoft Word 12" name="Originator"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_filelist.xml" rel="File-List"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_themedata.thmx" rel="themeData"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_colorschememapping.xml" rel="colorSchemeMapping"&gt; &lt;style&gt; &lt;!-- /* Font Definitions \*/ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"\\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions \*/ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions \*/ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} --&gt; &lt;/style&gt;

--测试字符串&lt;?xml:namespace prefix = o /&gt;

declare @inStr varchar(**800**)

set @inStr='123&lt;和这个&gt;56&lt;7&gt;8'

--测试语句

declare @str varchar(**800**)

set @str='var data = "'+@inStr+'";var reCat = /&lt;\[^&gt;]+&gt;/gi;data.replace(reCat,"");'

declare @object int

declare @r varchar(**800**)

exec sp\_OACreate 'MSScriptControl.ScriptControl',@object output

exec sp\_OASetProperty @object, 'Language','javascript'

exec sp\_OAMethod @object, 'eval', @r out,@str

select @r

--输出

/\*123568\*/

```
如果是Oracle则简单很多，可以直接用SQL语句
```

Select REGEXP\_REPLACE('123&lt;和这个&gt;56&lt;7&gt;8','&lt;\[^&gt;]+&gt;', '') as r FROM dual;  
/*  
R  
123568  
\*/

```

```

&lt;meta content="Word.Document" name="ProgId"&gt; &lt;meta content="Microsoft Word 12" name="Generator"&gt; &lt;meta content="Microsoft Word 12" name="Originator"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_filelist.xml" rel="File-List"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_themedata.thmx" rel="themeData"&gt; &lt;link href="file:///C:%5CUsers%5Cjinjazz%5CAppData%5CLocal%5CTemp%5Cmsohtmlclip1%5C01%5Cclip\_colorschememapping.xml" rel="colorSchemeMapping"&gt; &lt;style&gt; &lt;!-- /* Font Definitions \*/ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:"\\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions \*/ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions \*/ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} &lt;/style&gt;