给出以下多行字符串:
WHERE ([EXTENT1].[MY_ID] IN (151,152,214,218,931,932,933,1067,1412,1414,13807,14347,14349,14446)) AND ([EXTENT1].[MY_OTHER_ID] IN (14264, 14335, 14385, 14398, 14603, 14650, 15164, 15374)) AND ([EXTENT2].[PERSON_ID] IN (28,933,14446,179,152,14349,14347,933,130,218,933,1067,931,151,214,152,933,145,931,145,5809,14347,14349,14349,1414,142,1412,179,152,14347,152,90,13807,932,931))
) AS [FILTER1]
GROUP BY [K1], [K2]
) AS [GROUPBY1]
我想为[MY_ID]提取IN子句的带括号的值。我可以使用以下正则表达式(?<=\.\[MY_ID\].*IN.*\().*
来截断字符串的第一部分并返回:
151,152,214,218,931,932,933,1067,1412,1414,13807,14347,14349,14446)) AND ([EXTENT1].[MY_OTHER_ID] IN (14264, 14335, 14385, 14398, 14603, 14650, 15164, 15374)) AND ([EXTENT2].[PERSON_ID] IN (28,933,14446,179,152,14349,14347,933,130,218,933,1067,931,151,214,152,933,145,931,145,5809,14347,14349,14349,1414,142,1412,179,152,14347,152,90,13807,932,931))
但我无法弄清楚如何让它在第一次结束时停止。)
我所追求的是:151,152,214,218,931,932,933,1067,1412,1414,13807,14347,14349,14446
正则表达式最终将与.NET正则表达式引擎一起使用。
答案 0 :(得分:1)
尝试使用以下正则表达式。
正则表达式: (?<=\[MY_ID\] IN \()[^)]*
<强>解释强>
(?<=\[MY_ID\] IN \()
将look behind
[MY_ID] IN (
[^)]*
将匹配所有内容,直至满足)
,这标志着括号的结束。
<强> Regex101 Demo 强>
答案 1 :(得分:1)
由于.NET正则表达式支持重复的组,您可以使用
\.\[MY_ID]\s*IN\s*\(((?:,?(\d+))+)
并获取Group 1值(将为151,152,214,218,931,932,933,1067,1412,1414,13807,14347,14349,14446
)或将Group 2捕获集合中的所有捕获作为数组/列表。
请参阅regex demo
说明:
\.\[MY_ID]
- 文字.[MY_ID]
\s*
- 0+空白IN\s*
- IN
字后跟0 +空格\(
- 打开文字(
((?:,?(\d+))+)
- 第1组捕获1+序列:
,?
- 一个或零逗号(\d+)
- 第2组捕获1位数字。这是一个C# demo:
var s = "WHERE ([EXTENT1].[MY_ID] IN (151,152,214,218,931,932,933,1067,1412,1414,13807,14347,14349,14446)) AND ([EXTENT1].[MY_OTHER_ID] IN (14264, 14335, 14385, 14398, 14603, 14650, 15164, 15374)) AND ([EXTENT2].[PERSON_ID] IN (28,933,14446,179,152,14349,14347,933,130,218,933,1067,931,151,214,152,933,145,931,145,5809,14347,14349,14349,1414,142,1412,179,152,14347,152,90,13807,932,931))\n ) AS [FILTER1]\n GROUP BY [K1], [K2]\n) AS [GROUPBY1]";
var pattern = @"\.\[MY_ID]\s*IN\s*\(((?:,?(\d+))+)";
var matches = Regex.Matches(s, pattern);
var res1 = matches
.Cast<Match>().Select(p => p.Groups[2].Captures) // Get a list of ind. numbers
.ToList();
var res2 = matches
.Cast<Match>().Select(p => p.Groups[1].Value) // Get the whole substring
.ToList();
foreach (var coll in res1)
foreach (var v in coll)
Console.WriteLine(v);
Console.WriteLine("Ex. 2");
foreach (var v2 in res2)
Console.WriteLine(v2);