C#正则表达式斯普利特 - 逗号以外报价 [英] C# Regex Split - commas outside quotes

查看:97
本文介绍了C#正则表达式斯普利特 - 逗号以外报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了很多的字符串(SQL code段,实际上),格式如下:

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

我本来用一个简单的斯普利特(''),但由于最后一个参数里面的那个逗号输出我需要使用正则表达式得到的是造成混乱它。问题是,我仍然经常EX pressions相当noobish我似乎无法破解为主的模式,因为字符串内数字及字母数字参数,可以在任何时间存在的...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆