T-SQL 正则表达式(CLR 实现)

xman_78tom 2010-06-23 10:01:58
加精
创建程序集 ClrRegExClass.cs,
并使用 C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library ClrRegExClass.cs 编译为 ClrRegExClass.DLL 文件。

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;

public partial class RegExp
{
// 验证字符串中是否包含与指定的匹配模式一致的字符串
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlBoolean RegExIsMatch(SqlString expression, SqlString pattern)
{
return new SqlBoolean(Regex.IsMatch(expression.ToString(), pattern.ToString()));
}

// 替换字符串中与指定的匹配模式一致的字符串
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExReplace(SqlString expression, SqlString pattern, SqlString replacement)
{
return new SqlString(Regex.Replace(expression.ToString(), pattern.ToString(), replacement.ToString()));
}

// 提取字符串中与指定的匹配模式一致的字符串
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlString RegExSubstring(SqlString expression, SqlString pattern, SqlInt32 position, SqlInt32 occurrence)
{
if (expression.ToString().Length < position) return new SqlString("");
if (position <= 0) position = 1;
if (occurrence <= 0) occurrence = 1;

Match m = Regex.Match(expression.ToString().Substring((int) position - 1),pattern.ToString());
for (int i = 1; i < (int)occurrence; i++)
{
m = m.NextMatch();
if (!m.Success) return new SqlString("");
}

return new SqlString(m.ToString());
}

// 计算字符串中与指定的匹配模式一致的字符串的数目
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlInt32 RegExCount(SqlString expression, SqlString pattern, SqlInt32 position)
{
if (expression.ToString().Length < position) return 0;
if (position <= 0) position = 1;

MatchCollection ms = Regex.Matches(expression.ToString().Substring((int)position - 1), pattern.ToString());

return new SqlInt32(ms.Count);
}

// 查找字符串中与指定的匹配模式一致的字符串的位置
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
public static SqlInt32 RegExIndex(SqlString expression, SqlString pattern, SqlInt32 position, SqlInt32 occurrence)
{
if (expression.ToString().Length < position) return 0;
if (position <= 0) position = 1;
if (occurrence <= 0) occurrence = 1;

Match m = Regex.Match(expression.ToString().Substring((int)position - 1), pattern.ToString());
for (int i = 1; i < (int)occurrence; i++)
{
m = m.NextMatch();
if (!m.Success) return 0;
}

return new SqlInt32(m.Index + 1);
}
}


将程序集 ClrRegExClass.DLL 导入 SQL Server,并创建相应的函数。

// 启用 SQL Server 的 CLR 支持
exec sp_configure 'clr enabled',1;
go
reconfigure
go

// 导入程序集
if exists (select * from sys.assemblies where name='RegExp')
drop assembly RegExp;
go
create assembly RegExp authorization dbo
from 'fullpath\ClrRegExClass.dll'
with permission_set=safe;
go

// 创建函数
// @expression 为输入的字符串;@pattern 为正则表达式;
// @position 为字符串开始的位置;@occurrence 为与指定的匹配模式一致的字符串出现的次数

if object_id('dbo.regex_ismatch','FS') is not null
drop function dbo.regex_ismatch;
go
create function dbo.regex_ismatch
(@expression nvarchar(max), @pattern nvarchar(max))
returns bit with returns null on null input
as external name RegExp.RegExp.RegExIsMatch;
go

-- 验证字符串是否以 [server] 开头
-- select dbo.regex_ismatch('[server].[database].[schema].[object]','^\[server\]');

if object_id('dbo.regex_replace','FS') is not null
drop function dbo.regex_replace;
go
create function dbo.regex_replace
(@expression nvarchar(max), @pattern nvarchar(max), @replacement nvarchar(max))
returns nvarchar(max) with returns null on null input
as external name RegExp.RegExp.RegExReplace;
go

-- 将字符串中 [...] 替换为 "..."
-- select dbo.regex_replace('[server].[database].[schema].[object]','\[([\w]*)\]','"$1"');

if object_id('dbo.regex_substring','FS') is not null
drop function dbo.regex_substring;
go
create function dbo.regex_substring
(@expression nvarchar(max), @pattern nvarchar(max), @position int, @occurrence int)
returns nvarchar(max) with returns null on null input
as external name RegExp.RegExp.RegExSubstring;
go

-- 提取字符串中与 [...] 模式匹配的第二次出现的字符串
-- select dbo.regex_substring('[server].[database].[schema].[object]','\[\w*\]',1,2);

if object_id('dbo.regex_count','FS') is not null
drop function dbo.regex_count;
go
create function dbo.regex_count
(@expression nvarchar(max), @pattern nvarchar(max), @position int)
returns int with returns null on null input
as external name RegExp.RegExp.RegExCount;
go

-- 计算字符串中与 [...] 模式匹配的字符串的数目
-- select dbo.regex_count('[server].[database].[schema].[object]','\[\w*\]',1);

if object_id('dbo.regex_index','FS') is not null
drop function dbo.regex_index;
go
create function dbo.regex_index
(@expression nvarchar(max), @pattern nvarchar(max), @position int, @occurrence int)
returns int with returns null on null input
as external name RegExp.RegExp.RegExIndex;
go

-- 查询字符串中与 [...] 模式匹配的第二次出现的字符串开始的位置
-- select dbo.regex_index('[server].[database].[schema].[object]','\[\w*\]',1,2);


(希望大家指正)
...全文
4387 243 打赏 收藏 转发到动态 举报
写回复
用AI写文章
243 条回复
切换为时间正序
请发表友善的回复…
发表回复
-布谷鸟- 2012-12-26
  • 打赏
  • 举报
回复
期待着微软将这个功能直接集成进去,免得众生为此点灯熬油、展转反侧、彻夜难眠、苦思冥想、劳筋伤骨、茶饭不思、费尽360000000000000000个脑细胞...
cuibo1001 2012-11-21
  • 打赏
  • 举报
回复
很强大
huawenxue 2012-11-08
  • 打赏
  • 举报
回复
主要是来学习的,主要是努力
kiyukawa 2012-07-13
  • 打赏
  • 举报
回复
非常好的东西,收藏中!!!
我想静静0v0 2012-03-30
  • 打赏
  • 举报
回复
纠结....太难了
thq851211 2011-08-15
  • 打赏
  • 举报
回复
学习了 顶一下 !!!
caomeng2010 2011-08-09
  • 打赏
  • 举报
回复
积分,积分
petrie 2011-08-04
  • 打赏
  • 举报
回复
哈哈 0804 雪中送炭
petrie 2011-08-04
  • 打赏
  • 举报
回复
完全不懂!
joyhen 2010-12-14
  • 打赏
  • 举报
回复

灰常牛X啊
h289533672 2010-06-30
  • 打赏
  • 举报
回复
ding
appleller 2010-06-30
  • 打赏
  • 举报
回复
BBBBBBBBBDDDDDDDD
zxj828282 2010-06-30
  • 打赏
  • 举报
回复
top top top
cpurle 2010-06-30
  • 打赏
  • 举报
回复
不错 感觉很有用 顶
tttzzzyyy 2010-06-29
  • 打赏
  • 举报
回复
学习中!
solidz 2010-06-29
  • 打赏
  • 举报
回复
顶一下
juckxu 2010-06-29
  • 打赏
  • 举报
回复
GOOOD
barrylord 2010-06-29
  • 打赏
  • 举报
回复
好好学习学习
hjplover 2010-06-29
  • 打赏
  • 举报
回复
ding din dinb
kangzhan20 2010-06-29
  • 打赏
  • 举报
回复
支持。顶。。
加载更多回复(209)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧