Reputation: 442
I am trying to create a regex to get a output for below string, in sets of 4 comma separated values. I have tried something but it only selects each comma separated value. I don't know how to get the desired output.
The regex I tried:
".*?"(?=,|$)
Data:
"T","Success","2","2","T","Success","6458960","1","F,"You do not have sufficient credit.","6458962","1"
Desired out:
"T","Success","2","2"
"T","Success","6458960","1"
"F,"You do not have sufficient credit.","6458962","1"
Update: "The F is in double quote too, it was a typo, Sorry!"
"T","Success","2","2","T","Success","6458960","1","F","You do not have sufficient credit.","6458962","1"
Upvotes: 1
Views: 1041
Reputation: 1101
You can try this regex,but the regex has a problem:you need to trim then last Comma
,and even there are much more Comma
in string, not multiple of 4 Comma
,it can also works fine
string patten = @"(?<=^(?:(?:[^,]*,){4})+)";
string text = @"""T"",""Success"",""2"",""2"",""T"",""Success"",""6458960"",""1"",""F,""You do not have sufficient credit."",""6458962"",""1""";
foreach (var tmp in Regex.Split(text, patten))
{
Console.WriteLine(tmp.TrimEnd(','));
}
Upvotes: 0
Reputation: 2747
I would avoid regex unless you really need it, generally they can be harder to understand.
For fun here is a Linq solution:
var data = @"""T"",""Success"",""2"",""2"",""T"",""Success"",""6458960"",""1"",""F,""You do not have sufficient credit."",""6458962"",""1""";
var res = data.Split(',')
.Select((x ,i) => new { Pos = i / 4, Val = x })
.GroupBy(x => x.Pos)
.Select(g => string.Join(",", g.Select(x => x.Val)));
Upvotes: 0
Reputation: 89221
((?:"[^"]*"|[^,"])*(?:,(?:"[^"]*"|[^,"])*){3}),?
(?:"[^"]*"|[^,"])*
will match a values between commas, optionally quoted. Quotes are escaped as ""
.
(X(?:,X){3}),?
where X
is pattern #1, will match a sequence of four comma-separated values, and an optional trailing comma. The comma is necessary to correctly match blank values (,,foo,
).
If the values are guaranteed to always have quotes, you can remove |[^,"]
and ,?
.
Upvotes: 0
Reputation: 2004
You could use following regex, but only if the F is also in enclosed quotes:
((?:".+",){3}(?:".+"))
This results in:
MATCH 1
1. [0-21] "T","Success","2","2"
MATCH 2
1. [22-49] "T","Success","6458960","1"
MATCH 3
1. [50-104] "F","You do not have sufficient credit.","6458962","1"
If the data is really in this inconsistent form, you have to parse it manually or modify the regex with an or condition.
Upvotes: 1
Reputation: 460228
You just need String.Split
and this:
string[] fields = str.Split(',');
for (int i = 0; i < fields.Length; i += 4)
Console.WriteLine(string.Join(",", fields.Skip(i).Take(4)));
Output:
"T","Success","2","2"
"T","Success","6458960","1"
"F,"You do not have sufficient credit.","6458962","1"
This presumes that this is not really csv data. Otherwise i would suggest to use a real CSV parser that supports quoting characters. But it seems that they are broken anyway(,"1","F,.
, so F is not enclosed in quotes).
Upvotes: 1