我认为您的正则表达式不必要地复杂,并且嵌套的量词会导致灾难性的回溯。尝试以下操作:
^[^"]*(?<Entry>(?>"(?>[^"]+|"")*"),)*(?<LastEntry>(?>"(?>[^"]+|"")*"))$
解释:
^ # Start of string
[^"]* # Optional non-quotes
(?<Entry> # Match group 'entry'
(?> # Match, and don't allow backtracking (atomic group):
" # a quote
(?> # followed by this atomic group:
[^"]+ # one or more non-quote characters
| # or
"" # two quotes in a row
)* # repeat 0 or more times.
" # Then match a closing quote
) # End of atomic group
, # Match a comma
)* # End of group 'entry'
(?<LastEntry> # Match the final group 'lastEntry'
(?> # same as before
" # quoted field...
(?>[^"]+|"")* # containing non-quotes or double-quotes
" # and a closing quote
) # exactly once.
) # End of group 'lastEntry'
$ # End of string
这也应该适用于整个文件,因此在正则表达式匹配之前,您不必在下一行之后添加一行,并且您不必替换换行符:
Dim RegexObj As New Regex("^[^""]*(?<Entry>(?>""(?:[^""]+|"""")*""),)*(?<LastEntry>(?>""(?:[^""]+|"""")*""))$", RegexOptions.Multiline)
Dim MatchResults As Match = RegexObj.Match(SubjectString)
While MatchResults.Success
' now you can access MatchResults.Groups("Entry").Captures and
' MatchResults.Groups("LastEntry")
MatchResults = MatchResults.NextMatch()
End While