我发现了。使用 C#,您可以使用匹配集合中的属性 Captures。
使用正则表达式:
\((([^,]),?)+\)
做:
string text = "(a,b,c,d,e)";
Regex rgx = new Regex("\\((([^,]),?)+\\)");
MatchCollection matches = rgx.Matches(text);
然后你在 matchcollection 中有 1 个包含以下 3 个组的项目:
[0]: \((([^,]),?)+\) => (a,b,c,d,e)
[1]: ([^,]),?+ => value and optional comma, eg. a, or b, or e
[2]: [^,] => value only, eg. a or b or ...
组内捕获的列表通过量化器存储每个提取的值。所以使用组 [2] 和捕获来获取所有值。
所以解决方案是:
string text = "(a,b,c,d,e)";
Regex rgx = new Regex("\\((([^,]),?)+\\)");
MatchCollection matches = rgx.Matches(text);
//now get out the captured calues
CaptureCollection captures = matches[0].Groups[2].Captures;
//and extract them to list
List<string> values = new List<string>();
foreach (Capture capture in captures)
{
values.Add(capture.Value);
}