C#中,经常前pressions:如何解析逗号分隔值,其中一些值可能会被引用的字符串包含自己逗号 [英] C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas

查看:87
本文介绍了C#中,经常前pressions:如何解析逗号分隔值,其中一些值可能会被引用的字符串包含自己逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,使用正则表达式类,如何来解析逗号分隔值,其中一些值可能会被引用本身包含逗号的字符串?

 使用系统;
使用System.Text.RegularEx pressions;类示例
    {
    公共静态无效的主要()
    {
    字符串的myString =猫,狗,\\0 = OFF,1 = ON \\,狮,虎,R =红色,G =绿色,B =蓝色,承担;
    Console.WriteLine(\\ nmyString是... \\ n \\ t+的myString +\\ n);
    正则表达式的regex ​​=新的正则表达式(; |'?))*(=(\\| \\'?(小于?=,(\\\\))|(^ *(=,))|( (?&LT =)。*(=,?))|((小于?=,?)。?* $));
    匹配匹配= regex.Match(myString的);
    INT J = 0;
    而(match.Success)
    {
    Console.WriteLine(J ++ +\\ t+匹配);
    匹配= match.NextMatch();
    }
    }
    }

输出(部分)如下所示:

  0猫
1狗
20 = OFF
3 1 = ON
4狮子
5虎
6'R =红
7 G =绿色
8 B =蓝色
9熊

然而,期望的输出是:

  0猫
1狗
2 0 = OFF,1 = ON
3狮子
4虎
5 R =红色,G =绿色,B =蓝色
6熊


解决方案

尝试用这个表达式:

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


 正则表达式regexObj =新的正则表达式(@[^,\\ r \\ n] *|'[^'\\ r \\ n] *'| [^, \\ r \\ n] *);
    比赛matchResults = regexObj.Match(输入);
    而(matchResults.Success)
    {
        Console.WriteLine(matchResults.Value);
        matchResults = matchResults.NextMatch();
    }

。OUPUTS:




  • 0 = OFF,1 = ON



  • R =红色,G =绿色,B =蓝色


注意:此正则表达式的解决方案会为你的情况下工作,但是我建议你使用像 FileHelpers专业图书馆

In C#, using the Regex class, how does one parse comma-separated values, where some values might be quoted strings themselves containing commas?

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() ;
    		}
    	}
    }

Output (in part) appears as follows:

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

However, desired output is:

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

解决方案

Try with this Regex:

"[^"\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();
    }

Ouputs:

  • cat
  • dog
  • "0 = OFF, 1 = ON"
  • lion
  • tiger
  • 'R = red, G = green, B = blue'
  • bear

Note: This regex solution will work for your case, however I recommend you to use a specialized library like FileHelpers.

这篇关于C#中,经常前pressions:如何解析逗号分隔值,其中一些值可能会被引用的字符串包含自己逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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