sql server 的正则表达式拆分实现

发布于 2024-09-13 03:35:11 字数 1082 浏览 4 评论 0原文

我创建了一个函数来实现 sql 中的 regex.split 。这是代码:

private static IEnumerable<IndexedValue<T>> ToIndexedValue<T>(IEnumerable<T> list)
{
  int idx = 1;
  foreach (T value in list)
    yield return new IndexedValue<T>(++idx, value);
}
private struct IndexedValue<T>
{
  public int Index;
  public T Value;
  public IndexedValue(int index, T value)
  {
    Index = index;
    Value = value;
  }
}
[SqlFunction(FillRowMethodName = "FillSplit",
  TableDefinition = "[ID] int, [Value] nvarchar(max)")]
public static IEnumerable RegexSplit(SqlString input, SqlString pattern)
{
  if (input.IsNull)
    input = String.Empty;
  if (pattern.IsNull)
    pattern = String.Empty;
  try
  {
    return ToIndexedValue<string>(Regex.Split(input.Value, pattern.Value, Options));
  }
  catch
  {
    throw;
  }
}
public static void FillSplit(object obj, out int id, out SqlString value)
{
  IndexedValue<string> iv = (IndexedValue<string>)obj;
  id = iv.Index;
  value = iv.Value;
}

但是,当我尝试它时,我得到 id 值,但得到空文本值。有人可以帮忙吗?

I have created a function to implement the regex.split in sql. Here's the code:

private static IEnumerable<IndexedValue<T>> ToIndexedValue<T>(IEnumerable<T> list)
{
  int idx = 1;
  foreach (T value in list)
    yield return new IndexedValue<T>(++idx, value);
}
private struct IndexedValue<T>
{
  public int Index;
  public T Value;
  public IndexedValue(int index, T value)
  {
    Index = index;
    Value = value;
  }
}
[SqlFunction(FillRowMethodName = "FillSplit",
  TableDefinition = "[ID] int, [Value] nvarchar(max)")]
public static IEnumerable RegexSplit(SqlString input, SqlString pattern)
{
  if (input.IsNull)
    input = String.Empty;
  if (pattern.IsNull)
    pattern = String.Empty;
  try
  {
    return ToIndexedValue<string>(Regex.Split(input.Value, pattern.Value, Options));
  }
  catch
  {
    throw;
  }
}
public static void FillSplit(object obj, out int id, out SqlString value)
{
  IndexedValue<string> iv = (IndexedValue<string>)obj;
  id = iv.Index;
  value = iv.Value;
}

However when i try it, i get id values but empty text values. Can someone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

探春 2024-09-20 03:35:11

给你(编译为 SQL CLR 程序集):

using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
  [SqlFunction]
  public static bool RegexMatch(string expr, string regex)
  {
    return Regex.IsMatch(expr, regex);
  }

  [SqlFunction]
  public static string RegexReplace(string expr, string regex, string replace)
  {
    return Regex.Replace(expr, regex, replace);
  }

  [SqlFunction(FillRowMethodName="GetToken", 
       TableDefinition="Value nvarchar(max)")]
  public static IEnumerable RegexSplit(string expr, string regex)
  {
    return Regex.Split(expr, regex);
  }

  public static void GetToken(object row, out string str)
  {
     str = (string) row;
  }
}

最初来自: microsoft sql相当于 mysql REGEXP

Here you go (compile as SQL CLR assembly):

using System.Collections;
using System.Text.RegularExpressions;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{
  [SqlFunction]
  public static bool RegexMatch(string expr, string regex)
  {
    return Regex.IsMatch(expr, regex);
  }

  [SqlFunction]
  public static string RegexReplace(string expr, string regex, string replace)
  {
    return Regex.Replace(expr, regex, replace);
  }

  [SqlFunction(FillRowMethodName="GetToken", 
       TableDefinition="Value nvarchar(max)")]
  public static IEnumerable RegexSplit(string expr, string regex)
  {
    return Regex.Split(expr, regex);
  }

  public static void GetToken(object row, out string str)
  {
     str = (string) row;
  }
}

Originally from: microsoft sql equivalent of mysql REGEXP

云淡月浅 2024-09-20 03:35:11

我怀疑您使用的正则表达式返回空字符串或空格的数组。您是否尝试过为表达式本身设置一个简单的命令行测试框架?

除了 Options 未定义之外,我看不出这段代码有什么问题。这是在其他地方定义的常量吗?

I suspect that the regular expression you're using is returning an array of empty strings or whitespace. Have you tried to set up a simple command line test frame for the expression itself?

I can't see anything wrong with this code except that Options is undefined. Is this a constant defined elsewhere?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文