C#,正则表达式:如何解析逗号分隔值,其中一些值可能是带引号的字符串,其中包含逗号 [英] C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas
本文介绍了C#,正则表达式:如何解析逗号分隔值,其中一些值可能是带引号的字符串,其中包含逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,使用 Regex
类,如何解析逗号分隔值,其中一些值可能是引号括起来的字符串本身包含逗号?
使用System;
using System.Text.RegularExpressions;
class示例
{
public static void Main()
{
string myString =cat,dog,\0 = = ON \,狮子,虎,'R =红,G =绿,B =蓝',熊
Console.WriteLine(\\\
myString is ... \\\
\t+ myString +\\\
);
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
20 = OFF
3 1 = ON
4 lion
5 tiger
6'R = red
7 G = green
8 B = blue'
9 bear
但是,所需的输出是:
$ b b
0 cat
1 dog
2 0 = OFF,1 = ON
3 lion
4 tiger
5 R = red,G = green,B = blue
6 bear
解决方案
尝试使用此正则表达式:
[^\r\\\
] [^'\r\\\
] *'| [^,\r\\\
] *
Regex regexObj = new Regex(@[^\r\\\
] *| ^ b \\ b匹配matchResults = regexObj.Match(input);
while(matchResults.Success)
{
Console.WriteLine(matchResults.Value);
matchResults = matchResults.NextMatch();
}
Ouputs:
- cat
- dog
- 0 =关闭,1 =开
- lion
- tiger
- '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#,正则表达式:如何解析逗号分隔值,其中一些值可能是带引号的字符串,其中包含逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文