SQL 2005使用正则表达式
SQL Server 2005支持用CLR语言(C# .NET、VB.NET)编写过程、触发器和函数,因此使得正则匹配,数据提取能够在SQL中灵活运用,大大提高了SQL处理字符串,文本等内容的灵活性及高效性。
操作步骤:
1.新建一个SQL Server项目(输入用户名,密码,选择DB),新建好后,可以在属性中更改的
2.新建一个类“RegexMatch.cs”,选择用户定义的函数
可以看到,该类为一个部分类:public partial class UserDefinedFunctions
现在可以在该类中写方法了,注意方法的属性为:[Microsoft.SqlServer.Server.SqlFunction]
现在类中增加以下两个方法:
是否匹配正则表达式
/// <summary>
/// 是否匹配正则表达式
/// </summary>
/// <param name="input">输入的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="ignoreCase">是否忽略大小写</param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static bool RegexMatch(string input, string pattern, bool ignoreCase)
{
bool isMatch = false;
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))
{
try
{
Match match = null;
if (ignoreCase)
match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
else
match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.Compiled);
if (match.Success)
isMatch = true;
}
catch { }
}
return isMatch;
}
/// 是否匹配正则表达式
/// </summary>
/// <param name="input">输入的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="ignoreCase">是否忽略大小写</param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static bool RegexMatch(string input, string pattern, bool ignoreCase)
{
bool isMatch = false;
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))
{
try
{
Match match = null;
if (ignoreCase)
match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
else
match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.Compiled);
if (match.Success)
isMatch = true;
}
catch { }
}
return isMatch;
}
获取正则表达式分组中的字符
/// <summary>
/// 获取正则表达式分组中的字符
/// </summary>
/// <param name="input">输入的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="groupId">分组的位置</param>
/// <param name="maxReturnLength">返回字符的最大长度</param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string GetRegexMatchGroups(string input, string pattern, int groupId, int maxReturnLength)
{
string strReturn = string.Empty;
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))
{
try
{
Match match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (match.Success && (groupId < match.Groups.Count))
{
strReturn = match.Groups[groupId].Value;
strReturn = (strReturn.Length <= maxReturnLength) ? strReturn : strReturn.Substring(0, maxReturnLength);
}
}
catch
{
return string.Empty;
}
}
return strReturn;
}
/// 获取正则表达式分组中的字符
/// </summary>
/// <param name="input">输入的字符串</param>
/// <param name="pattern">正则表达式</param>
/// <param name="groupId">分组的位置</param>
/// <param name="maxReturnLength">返回字符的最大长度</param>
/// <returns></returns>
[Microsoft.SqlServer.Server.SqlFunction]
public static string GetRegexMatchGroups(string input, string pattern, int groupId, int maxReturnLength)
{
string strReturn = string.Empty;
if (!string.IsNullOrEmpty(input) && !string.IsNullOrEmpty(pattern))
{
try
{
Match match = Regex.Match(input, pattern, RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (match.Success && (groupId < match.Groups.Count))
{
strReturn = match.Groups[groupId].Value;
strReturn = (strReturn.Length <= maxReturnLength) ? strReturn : strReturn.Substring(0, maxReturnLength);
}
}
catch
{
return string.Empty;
}
}
return strReturn;
}
3.下一步就是部署的问题了,点击项目右键--》部署即可
提示部署成功了,可以在数据库的标量值函数中多了这两个方法了。
使用方法和正常的SQL函数一样:
select dbo.RegexMatch('/Book/103.aspx','/book/(\d+).aspx','true')
消息 6263,级别 16,状态 1,第 1 行
禁止在 .NET Framework 中执行用户代码。启用 "clr enabled" 配置选项。
——出现此错误,配置下:
exec sp_configure 'clr enabled',1;
reconfigure with override;
是否匹配:
select dbo.RegexMatch('/Book/103.aspx','/book/(\d+).aspx','true')
返回1,表示匹配成功
select dbo.RegexMatch('/Book/103.aspx','/book/(\d+).aspx','false')
表示0,匹配失败(不忽略大小写)。
数据提取:
select dbo.GetRegexMatchGroups('/Book/103.aspx','/book/(\d+).aspx',1,50)
返回103,非常方便的提取。
注意:SQL中使用CLR时,尽量使用try catch…以免出现异常。
分类:
mssql
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器