问题,(?=(?:[^'] *'[^'] *')* [^'] * $)

时间:2011-07-01 04:05:05

标签: c# regex

C# Regex Split - commas outside quotes

var result = Regex.Split(samplestring, ",(?=(?:[^\"]*\"[^\"]*')*[^\"]*$)");

我无法理解它的工作原理。

具体来说,我不知道这里匹配的是什么?

",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)")
                     ^

是否意味着

(?=(?:[^\"]*\"[^\"]*')

的0或更多

更新示例输入

2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,"Corvallis, OR",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34

使用以下代码进行测试:

string samplestring = "2,1016,7/31/2008 14:22,Geoff Dalgas,6/5/2011 22:21,http://stackoverflow.com,\"Corvallis, OR\",7679,351,81,b437f461b3fd27387c5d8ab47a293d35,34";

2 个答案:

答案 0 :(得分:5)

这意味着组(?:[^']*'[^']*')匹配零次或多次。

,       // match one comma
(?=     // Start a positive lookAHEAD assertion
(?:     // Start a non-capturing group
[^']*   // Match everything but a single-quote zero or more times
'       // Match one single-quote
[^']*   // Match everything but a single-quote zero or more times
'       // Match one single-quote
)*      // End group and match it zero or more times
[^']*   // Match everything but a single-quote zero or more times
$)      // end lookAHEAD

答案 1 :(得分:0)

您可以在此网站上查看正则表达式并进行测试:

http://www.annuaire-info.com/outil-referencement/expression-reguliere/

;)玩得开心