sql server 的正则表达式拆分实现
我创建了一个函数来实现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
给你(编译为 SQL CLR 程序集):
最初来自: microsoft sql相当于 mysql REGEXP
Here you go (compile as SQL CLR assembly):
Originally from: microsoft sql equivalent of mysql REGEXP
我怀疑您使用的正则表达式返回空字符串或空格的数组。您是否尝试过为表达式本身设置一个简单的命令行测试框架?
除了
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?