这是.NET的Regex.Split中的错误吗? [英] Is this a bug in .NET's Regex.Split?
问题描述
我有两个正则表达式,用于 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表达式中使用捕获括号,所有捕获的文本都将包含在结果字符串数组中。
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.
这篇关于这是.NET的Regex.Split中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!