- 1、本文档共14页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
CLR实现SQL正则表达式
T-SQL 正则表达式(CLR 实现)创建程序集 ClrRegExClass.cs,并使用 C:\Windows\Microsoft.NET\Framework\v2.0.50727\csc.exe /target:library ClrRegExClass.cs 编译为 ClrRegExClass.DLL 文件。C# code
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)posi
您可能关注的文档
最近下载
- Howdoyoufeel_Howdoyoufeel?-碑林区-x-x微课公开课教案教学设计课件.pptx VIP
- 室外环境石材铺装施工工艺.doc VIP
- 化工园区事故应急池建设标准.doc
- 三年高考2024_2025高考地理真题分项汇编专题03地球上的大气含解析.doc VIP
- 《建筑结构》课程思政教学案例(一等奖).docx
- 《基于核心素养的初中语文“活动·探究”单元整体教学设计研究》课题研究方案.doc
- 四年级上册数学第二单元《练习三》教学课件(苏教版).pptx
- (最新)24年秋统编四年级语文上册习作:我的家人(精品课件).pptx
- 货币政策与财政政策配合的中国实践及经验.docx VIP
- 三年高考2024-2025高考地理真题分项汇编地球上的大气含解析.pdf VIP
文档评论(0)