我有一个看起来像这样的字符串:
'xxx',1,'yyy',0,'zzz,www',0,'','etc'
我想用逗号分隔,但问题在于包含它的元素(字符串)。 我可以按元素解析这个字符串元素,但最好使用像Regex这样的东西。
如果element是string的类型,则它总是在引号中。
预期结果:
xxx
1
yyy
0
zzz,www
0
<empty>
etc
用于拆分的Oryginal字符串: DA, 'SHT-1',3000.00,1500.00,1.00,1,3000.00,1500.00, '1.4301-10',7.900, *'1.4301','MAX,MIN-500','C'
代码:
string SampleText = @"DA,'SHT-1',3000.00,1500.00,1.00,1,3000.00,1500.00,'1.4301-10',7.900,
* '1.4301','MAX,MIN-500','C'";
// [1] Prepare for splitting (remove new lines, white spaces, etc);
SampleText = Regex.Replace(SampleText, @"\r\n?|\n|\*|\s", "");
// [2] replacing commas
MatchCollection mc = Regex.Matches(SampleText , "\'.*?\'");
foreach (Match mh in mc)
{
if (mh.Value.Contains(','))
{
SampleText = SampleText.Replace(mh.Value, mh.Value.Replace(",", "_"));
}
}
// splitting
string[] progHeader = SampleText.Split(new char[] { ',' });
我需要的是消除替换逗号的步骤。
答案 0 :(得分:0)
你可以尝试使用这样的东西
public static String[] SplitCsv(String value) {
if (Object.ReferenceEquals(null, value))
return null;
const Char quotation = '\'';
const Char separator = ',';
List<String> result = new List<String>();
Boolean inQuotation = false;
Boolean isStarted = false;
StringBuilder Sb = new StringBuilder();
foreach (Char Ch in value) {
if (inQuotation) {
Sb.Append(Ch);
inQuotation = Ch != quotation;
continue;
}
if (Ch == separator) {
result.Add(Sb.ToString());
Sb.Length = 0;
isStarted = true;
}
else {
Sb.Append(Ch);
isStarted = false;
inQuotation = Ch == quotation;
}
}
if (isStarted || (Sb.Length > 0))
result.Add(Sb.ToString());
return result.ToArray();
}
....
var test1 = SplitCsv("1,2,3"); // <- ["1", "2", "3"]
var test2 = SplitCsv("1,2,"); // <- ["1", "2", ""]
var test3 = SplitCsv("1,'2',3"); // <- ["1", "'2'", "3"]
var test4 = SplitCsv("1,'2,3'"); // <- ["1", "'2,3'"]
var test5 = SplitCsv("1,''"); // <- ["1", "''"]
答案 1 :(得分:0)
如果这只是一个练习:/('[^']*')|([^,']+)/g
看起来会这样(但匹配,而不是拆分)。