




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
T-SQL 正则表达式(CLR 实现)创建程序集 ClrRegExClass.cs,并使用 C:WindowsMicrosoft.NETFrameworkv2.0.50727csc.exe /target:library ClrRegExClass.cs 编译为 ClrRegExClass.DLL 文件。C# codeusing 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 code/ 启用 SQL Server 的 CLR 支持exec sp_configure clr enabled,1;goreconfigurego/ 导入程序集if exists (select * from sys.assemblies where name=RegExp) drop assembly RegExp;gocreate assembly RegExp authorization dbofrom fullpathClrRegExClass.dllwith permission_set=safe;go/ 创建函数/ expression 为输入的字符串;pattern 为正则表达式;/ position 为字符串开始的位置;occurrence 为与指定的匹配模式一致的字符串出现的次数if object_id(dbo.regex_ismatch,FS) is not null drop function dbo.regex_ismatch;gocreate function dbo.regex_ismatch(expression nvarchar(max), pattern nvarchar(max)returns bit with returns null on null inputas 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;gocreate function dbo.regex_replace(expression nvarchar(max), pattern nvarchar(max), replacement nvarchar(max)returns nvarchar(max) with returns null on null inputas 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;gocreate function dbo.regex_substring(expression nvarchar(max), pattern nvarchar(max), position int, occurrence int)returns nvarchar(max) with returns null on null inputas 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;gocreate function dbo.regex_count(expression nvarchar(max), pattern nvarchar(max), position int)returns int with returns null on null inputas 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;gocreate function dbo.regex_index(expression nvarchar(max), pattern nvarchar(max), position int, occurrence int)returns int with returns null on null inputas external name RegExp.RegExp.RegExIndex;go- 查询字符串中与 . 模式匹配的第二次出现的字符串开始的位置- select dbo.regex_index(server.database.schema.object,w*,1,2);以前我在网上看到一个更好的,比这个实用多了,楼主你这个RegExCount和RegExIndex一般不会用到,RegExSubstring更是不可能去使用,无意义的函数。看来看去只有一个RegExIsMatch会需要用到。好了,别说我不厚道,贴一下更强大的CLR函数(不是我写的),其中RegExGroups函数是精华,非常实用:C# codeusing System;using System.Data;using System.Data.SqlClient;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;using System.Text.RegularExpressions;using System.Collections;using System.Diagnostics.CodeAnalysis;public static partial class UserDefinedFunctions public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline; SqlFunction public static SqlBoolean RegexMatch( SqlChars input, SqlString pattern) Regex regex = new Regex( pattern.Value, Options ); return regex.IsMatch( new string( input.Value ) ); SqlFunction public static SqlChars RegexGroup( SqlChars input, SqlString pattern, SqlString name) Regex regex = new Regex(pattern.Value, Options); Match match = regex.Match(new string(input.Value); return match.Success ? new SqlChars(match.Groupsname.Value.Value) : SqlChars.Null; internal class MatchNode private int _index; public int Index get return _index; private string _value; public string Value get return _value; public MatchNode(int index, string value) _index = index; _value = value; internal class MatchIterator : IEnumerable private Regex _regex; private string _input; public MatchIterator(string input, string pattern) _regex = new Regex(pattern, UserDefinedFunctions.Options); _input = input; public IEnumerator GetEnumerator() int index = 0; Match current = null; do current = (current = null) ? _regex.Match(_input) : current.NextMatch(); if (current.Success) yield return new MatchNode(+index, current.Value); while (current.Success); SqlFunction(FillRowMethodName = FillMatchRow, TableDefinition = Index int,Text nvarchar(max) public static IEnumerable RegexMatches(SqlChars input, SqlString pattern) return new MatchIterator(new string(input.Value), pattern.Value); SuppressMessage(Microsoft.Design, CA1021:AvoidOutParameters) public static void FillMatchRow(object data, out SqlInt32 index, out SqlChars text) MatchNode node = (MatchNode)data; index = new SqlInt32(node.Index); text = new SqlChars(node.Value.ToCharArray(); internal class GroupNode private int _index; public int Index get return _index; private string _name; public string Name get return _name; private string _value; public string Value get return _value; public GroupNode(int index, string group, string value) _index = index; _name = group; _value = value; internal class GroupIterator : IEnumerable private Regex _regex; private string _input; public GroupIterator(string input, string pattern) _regex = new Regex(pattern, UserDefinedFunctions.Options); _input = input; public IEnumerator GetEnumerator() int index = 0; Match current = null; string names = _regex.GetGroupNames(); do index+; current = (current = null) ? _regex.Match(_input) : current.NextMatch(); if (current.Success) foreach (string name in names) Group group = current.Groupsname; if (group.Success) yield return new GroupNode( index, name, group.Value); while (current.Success); SqlFunction(FillRowMethodName = FillGroupRow, TableDefinition = Index int,Group nvarchar(max),Text nvarchar(max) public static IEnumerable RegexGroups(SqlChars inpu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026届山东省临沂市沂县物理八年级第一学期期末联考模拟试题含解析
- 瑞思课件谷歌链接
- 企业股份转让合同协议书股份转让合同协议书4篇
- 安全方面的培训证书课件
- 理财资金知识培训课件
- 甘南环保应急方案工程(3篇)
- 定西亮化工程方案(3篇)
- 安全文明校园培训内容课件
- 球磨安全操作培训课件
- 岑溪市明达建材有限公司建筑砌块制造项目环评报告
- 体育与健康主题班会
- 七年级《研究性学习》教案
- 小学生科普知识蜜蜂介绍PPT
- GB/T 5590-2008矿用防爆低压电磁起动器
- 船舶电气知识培训课件
- 院前急救理论知识考核试题题库及答案
- 归类总规则培训资料课件
- 监理工作管理制度汇编
- 豁免知情同意申请表【模板】
- DB33- 1015-2021《居住建筑节能设计标准》
- 基于MAXIMO的发电行业EAM解决方案
评论
0/150
提交评论