mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
Added sample that shows how to create CLR functions that expose .NEt Regular Expression classes.
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Microsoft.SqlServer.Server;
|
|
using System.Text.RegularExpressions;
|
|
|
|
/// <summary>
|
|
/// https://blogs.msdn.microsoft.com/sqlclr/2005/06/29/working-with-regular-expressions/
|
|
/// </summary>
|
|
public partial class RegEx
|
|
{
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static bool Match(string source, string pattern)
|
|
{
|
|
Regex r1 = new Regex(pattern);
|
|
return r1.Match(source).Success;
|
|
}
|
|
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static bool CompiledMatch(string source, string pattern)
|
|
{
|
|
return Regex.Match(source, pattern, RegexOptions.Compiled).Success;
|
|
}
|
|
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static string Substring(string source, string pattern)
|
|
{
|
|
Regex r1 = new Regex(pattern);
|
|
return r1.Match(source).Value;
|
|
}
|
|
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static string CompiledSubstring(string source, string pattern)
|
|
{
|
|
return Regex.Match(source, pattern, RegexOptions.Compiled).Value;
|
|
}
|
|
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static string Replace(string source, string pattern, string value)
|
|
{
|
|
return Regex.Replace(source, pattern, value);
|
|
}
|
|
|
|
[SqlFunction(IsDeterministic = true, IsPrecise = true)]
|
|
public static string CompiledReplace(string source, string pattern, string value)
|
|
{
|
|
return Regex.Replace(source, pattern, value, RegexOptions.Compiled);
|
|
}
|
|
}; |