Regular expression for separating by comma except quotation marks

What is a regular expression for comma separation (,), except for double quotation marks? For instance:

max,emily,john = ["max", "emily", "john"]

BUT

max,"emily,kate",john = ["max", "emily,kate", "john"]

Looking in C #: Regex.Split(string, "PATTERN-HERE");

Thank.

+5
source share
4 answers

Situations like this often require something other than regular expressions. They are elegant, but templates for handling this kind of thing are more complex than they are useful.

Instead, you can try something like this:

public static IEnumerable<string> SplitCSV(string csvString)
{
    var sb = new StringBuilder();
    bool quoted = false;

    foreach (char c in csvString) {
        if (quoted) {
            if (c == '"')
                quoted = false;
            else
                sb.Append(c);
        } else {
            if (c == '"') {
                quoted = true;
            } else if (c == ',') {
                yield return sb.ToString();
                sb.Length = 0;
            } else {
                sb.Append(c);
            }
        }
    }

    if (quoted)
        throw new ArgumentException("csvString", "Unterminated quotation mark.");

    yield return sb.ToString();
}

You probably need a few tweaks to exactly match the CSV specification, but the basic logic sounds.

+13
source

CSV, CSV cdhowie.

, , , Regex.Split():

(, !)

(?<=^(?:[^"]*"[^"]*")*[^"]*)  # assert that there is an even number of quotes before...
\s*,\s*                       # the comma to be split on...
(?=(?:[^"]*"[^"]*")*[^"]*$)   # as well as after the comma.

, , .

, , .NET, ( , "", ). , , , .

+1

, , , - .

     String[] cols = Regex.Split("max, emily, john", @"\s*,\s*");
     foreach ( String s in cols ) {
        Console.WriteLine(s);
     }
0

, , , . ( ) , s1, s2, s3 ...

:

"[^"]*"|(,)

"quoted strings". . 1, , , . SplitHere, SplitHere.

, (. -):

using System;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
class Program
{
static void Main()  {
string s1 = @"max,""emily,kate"",john";
var myRegex = new Regex(@"""[^""]*""|(,)");
string replaced = myRegex.Replace(s1, delegate(Match m) {
    if (m.Groups[1].Value == "") return m.Value;
    else return "SplitHere";
    });
string[] splits = Regex.Split(replaced,"SplitHere");
foreach (string split in splits) Console.WriteLine(split);
Console.WriteLine("\nPress Any Key to Exit.");
Console.ReadKey();
} // END Main
} // END Program

( ) , s1, s2, s3...

0

All Articles