C# 正则表达式拆分 - 引号外的逗号 [英] C# Regex Split - commas outside quotes
问题描述
我得到了很多字符串(实际上是 SQL 代码段),格式如下:
I got quite a lot of strings (segments of SQL code, actually) with the following format:
('ABCDEFG', 123542, 'XYZ 99,9')
并且我需要使用 C# 拆分此字符串以获得:
and i need to split this string, using C#, in order to get:
- 'ABCDEFG'
- 123542
- 'XYZ 99,9'
我最初使用的是一个简单的 Split(',')
,但是由于最后一个参数中的逗号导致了输出的严重破坏,我需要使用 Regex 来获取它.问题是我在正则表达式方面仍然很菜鸟,我似乎无法破解该模式,主要是因为在该字符串中,数字和字母数字参数可能随时存在...
I was originally using a simple Split(',')
, but since that comma inside the last parameter is causing havoc in the output i need to use Regex to get it. The problem is that i'm still quite noobish in regular expressions and i can't seem to crack the pattern mainly because inside that string both numerical and alpha-numerical parameters may exist at any time...
我可以用什么来根据引号外的每个逗号分割该字符串?干杯
What could i use to split that string according to every comma outside the quotes? Cheers
推荐答案
您可以拆分所有逗号,这些逗号后面有偶数个引号,使用以下正则表达式查找它们:
You could split on all commas, that do have an even number of quotes following them , using the following Regex to find them:
",(?=(?:[^']*'[^']*')*[^']*$)"
你会喜欢它
var result = Regex.Split(samplestring, ",(?=(?:[^']*'[^']*')*[^']*$)");
这篇关于C# 正则表达式拆分 - 引号外的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!