开发者

regex split implementation for sql server

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

private static IEnumerable&开发者_开发知识库lt;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?


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜