我有以下正则表达式:
@"{thing:(?:((\w)\2*)([^}]*?))+}"
Run Code Online (Sandbox Code Playgroud)
我用它来查找字符串中的匹配项:
MatchCollection matches = regex.Matches(string);
IEnumerable formatTokens = matches[0].Groups[3].Captures
.OfType<Capture>()
.Where(i => i.Length > 0)
.Select(i => i.Value)
.Concat(matches[0].Groups[1].Captures.OfType<Capture>().Select(i => i.Value));
Run Code Online (Sandbox Code Playgroud)
这用来产生我想要的结果; 但是,我的目标已经改变了.这是现在所需的行为:
假设输入的字符串是'stuff/{thing:aa/bb/cccc} {thing:cccc}'
我想 formatTokens是:
formatTokens[0] == "aa/bb/cccc"
formatTokens[1] == "cccc"
Run Code Online (Sandbox Code Playgroud)
现在,这是我得到的:
formatTokens[0] == "/"
formatTokens[1] == "/"
formatTokens[2] == "cccc"
formatTokens[3] == "bb"
formatTokens[4] == "aa"
Run Code Online (Sandbox Code Playgroud)
请特别注意,即使输入两次,"cccc"也不会出现两次.
我认为问题是1)正则表达式中的重新捕获和2)concat配置(从我希望所有内容分离时),但到目前为止,我还没有找到产生我想要的组合.有人能否对正确的正则表达式/ concat组合有所了解,以产生上述所需的结果?