一尘不染

C#,正则表达式:如何解析逗号分隔的值,其中某些值可能用引号引起来,字符串本身包含逗号

c#

在C#中,使用Regex该类,如何解析逗号分隔的值,其中某些值可能被引号括起来的包含逗号的字符串本身?

using System ;
using System.Text.RegularExpressions ;

class  Example
    {
    public static void Main ( )
        {
        string  myString  =  "cat,dog,\"0 = OFF, 1 = ON\",lion,tiger,'R = red, G = green, B = blue',bear" ;
        Console.WriteLine ( "\nmyString is ...\n\t" + myString + "\n" ) ;
        Regex   regex  =  new Regex  (  "(?<=,(\"|\')).*?(?=(\"|\'),)|(^.*?(?=,))|((?<=,).*?(?=,))|((?<=,).*?$)"  )  ;
        Match   match  =  regex.Match ( myString ) ;
        int j = 0 ;
        while ( match.Success )
            {
            Console.WriteLine ( j++ + " \t" + match ) ;
            match  =  match.NextMatch() ;
            }
        }
    }

输出(部分)如下所示:

0       cat
1       dog
2       "0 = OFF
3        1 = ON"
4       lion
5       tiger
6       'R = red
7        G = green
8        B = blue'
9       bear

但是, 所需的 输出是:

0       cat
1       dog
2       0 = OFF, 1 = ON
3       lion
4       tiger
5       R = red, G = green, B = blue
6       bear

阅读 223

收藏
2020-05-19

共1个答案

一尘不染

尝试使用此正则表达式:

"[^"\r\n]*"|'[^'\r\n]*'|[^,\r\n]*

    Regex regexObj = new Regex(@"""[^""\r\n]*""|'[^'\r\n]*'|[^,\r\n]*");
    Match matchResults = regexObj.Match(input);
    while (matchResults.Success) 
    {
        Console.WriteLine(matchResults.Value);
        matchResults = matchResults.NextMatch();
    }

  • “ 0 = OFF,1 = ON”
  • 狮子
  • ‘R =红色,G =绿色,B =蓝色’

注意:
此正则表达式解决方案适用于您的情况,但是我建议您使用专用的库,例如FileHelpers

2020-05-19