我有一个基于 .net 正则表达式函数的 SQL CLR 函数,用于按照正则表达式来拆分值。在其中一种情况下,我使用该函数来通过 |
来拆分值。问题是我发现其中一个值有双重的 ||
。由于我确定第二个值(右值)是一个数字,我知道第二个 |
是第一个值(左值)的一部分。
我有:
慂||2215
应该将它拆分为:
慂|
2215
我正在使用这个表达式[|]
进行拆分。我认为为了使其工作,我需要使用零宽度负向先行断言
。但是当我使用(?![|])[|]
进行分割时,我得到了:
慂||2215
如果我使用向后查找的方法,例如: (?<![|])[|]
,我将得到:
慂
|2215
我需要管道符号成为第一个值的一部分。有人可以帮助我吗?只寻求正则表达式解决方案,因为目前无法更改应用程序。
如果需要,这是函数:
/// <summary>
/// Splits an input string into an array of substrings at the positions defined by a regular expression pattern.
/// Index of each value is returned.
/// </summary>
/// <param name="sqlInput">The source material</param>
/// <param name="sqlPattern">How to parse the source material</param>
/// <returns></returns>
[SqlFunction(FillRowMethodName = "FillRowForSplitWithOrder")]
public static IEnumerable SplitWithOrder(SqlString sqlInput, SqlString sqlPattern)
{
string[] substrings;
List<Tuple<SqlInt64, SqlString>> values = new List<Tuple<SqlInt64, SqlString>>(); ;
if (sqlInput.IsNull || sqlPattern.IsNull)
{
substrings = new string[0];
}
else
{
substrings = Regex.Split(sqlInput.Value, sqlPattern.Value);
}
for (int index = 0; index < substrings.Length; index++)
{
values.Add(new Tuple<SqlInt64, SqlString>(new SqlInt64(index), new SqlString(substrings[index])));
}
return values;
}
(?:(?=[\|])|(?<![\|]))[^\|]*
。 - Cleptus|
上时,目前建议使用的\|(?!\|)
是足够的。 - Wiktor Stribiżew