这是.NET的正则表达式引擎一个错误?这是、错误、引擎、正则表达式

2023-09-03 05:31:28 作者:纸鸢

我有两个定期EX pressions标准,使用 Regex.Split

I have two regular expressions, for use with Regex.Split:

(?<=\G[^,],[^,],)

(?<=\G([^,],){2})

在分割字符串A,B,C,D,E,F,G,,第一个结果是:

When splitting the string "A,B,C,D,E,F,G,", the first one results in:

A,B, 
C,D, 
E,F, 
G, 

和在第二结果

A,B, 
A, 
C,D, 
C, 
E,F, 
E, 
G, 

这是怎么回事呢?我认为,(X){2} 总是等同于 XX ,但我不知道了。在我的实际问题,我需要做这样的事情更复杂了不少,而我需要做的是69倍,所以只是重复的模式不太理想。

What is going on here? I thought that (X){2} was always equivalent to XX, but I'm not sure anymore. In my actual problem, I need to do something like quite a bit more complex, and I need to do it sixty nine times, so just repeating the pattern is less than ideal.

推荐答案

从为的 Regex.Split

如果捕获括号在一个Regex.Split EX pression使用,任何捕获文本被包含在生成的字符串数组。

If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array.

内部括号捕获。尝试使用(?:[^])。而不是

The internal parentheses are capturing. Try using (?:[^,],) instead.