Search code examples
regexstringd

D: split string by comma, but not quoted string


I need to split string by comma, that not quoted like: foo, bar, "hello, user", baz

to get:

foo
bar
hello, user
baz

Solution

  • Using std.csv:

    import std.csv;
    import std.stdio;
    
    void main()
    {
        auto str = `foo,bar,"hello, user",baz`;
    
        foreach (row; csvReader(str))
        {
            writeln(row);
        }
    }
    

    Application output:

    ["foo", "bar", "hello, user", "baz"]
    

    Note that I modified your CSV example data. As std.csv wouldn't correctly parse it, because of space () before first quote (").