2

The question is a little confusing, so I will explain with an example. I am not using any language in particular.

I have the following string:

0,1,"foo","blue,yellow,red",27

I need to create an array of these comma-delimited values, but as you can see, the forth item (index 3) is a string with quotes that also contains commas.

I need to get that string as one value, with the quotes intact like so:

[0, 1, "foo", "blue,yellow,red", 27]

Splitting on the commas wont help me, as it will also split the string item. How would I go about parsing this comma-delimited string into the list of items.

4
  • 1
    You forgot to tag which programming language you are using. Commented Jun 5, 2013 at 2:09
  • Sorry, I just modified my question to say that I am not using any particular language. So answers in any language will do as long as they show the theory. Commented Jun 5, 2013 at 2:10
  • possible duplicate of regular expression should split , that are contained outside the double quotes in a CSV file? Commented Jun 5, 2013 at 2:13
  • 1
    I'd look at using a JSON parser. Slap [] on the string and then parse. (You sure it's not JSON you're parsing anyway?) Commented Jun 5, 2013 at 3:15

3 Answers 3

7

I don't know what language you are targeting, but the general approach is to read one character at a time, splitting at commas as usual. But if you encounter a " as the first character of a new item, you set a flag (like in_quotes). If that flag is set, you read all characters until the next ", at which time you set the flag to false.

Sign up to request clarification or add additional context in comments.

Comments

2

I'll suggest using strtok using comma as the field separator. However, if the first character in the string is a double quote, you should use " as the field separator.

If I assume that what you showed is a struct, I wrote the code in C to print the output on separate lines:

#include <stdio.h>
#include <string.h>

int main()
{
    char str[] = "0,1,\"foo\",\"blue,yellow,red\",27";

    printf ( "Input string: %s\n", str );

    char * substr;
    char * str_itr = str;
    char comma[] = ",";
    char quote[] = "\"";

    substr = strtok ( str_itr, comma );
    if ( substr )
        printf ( "%s\n", substr );

    substr = strtok ( NULL, comma );
    if ( substr )
        printf ( "%s\n", substr );

    substr = strtok ( NULL, quote );
    if ( substr )
        printf ( "%s\n", substr );

    substr = strtok ( NULL, quote );
    substr = strtok ( NULL, quote );
    if ( substr )
        printf ( "%s\n", substr );

    substr = strtok ( NULL, comma );
    if ( substr )
        printf ( "%s\n", substr );

    return ( 0 );
}

Comments

0

With Perl:

my $s = '0,1,"foo","blue,yellow,red",27';
my @l = grep {defined $_} split(/("[^"]*")|,/, $s);
print join("-" , @l), "\n";

Output:

0-1--"foo"---"blue,yellow,red"--27

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.