Is this a bug in .NET's Regex.Split?

扶醉桌前 提交于 2019-12-17 21:04:42

问题


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

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

and

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

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

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

and the second results in:

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

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.


回答1:


From the documentation for 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.




回答2:


From docs:

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

You have a capture group in your second expression. Try non-capturing parens:

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


来源:https://stackoverflow.com/questions/19286238/is-this-a-bug-in-nets-regex-split

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!