in reply to Re: CSV file
in thread CSV file

That regex doesn't even work on the OP's data, and neither will it correctly split strings which contain delimiters within quotes. Read the top node again and you'll probably realise that a regular expression is not the right solution here.


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Replies are listed 'Best First'.
Re^3: CSV file
by Moron (Curate) on Oct 31, 2005 at 16:17 UTC
    I missed the fact there is a comma in the name field too and modified the regular expression accordingly. Someone more handy at regexp's could probably factorise this more succinctly, however.

    But if we analyse the logic behind your argument we get "an error in analysing the requirement implies that all technical solutions resembling it must be wrong" - which I would hope reveals why I disagree with the argument, while remaining open-minded about variety of possible solutions.

    -M

    Free your mind

      Your regex still doesn't do what the OP is asking for, namely to split the line into the comma-delimited fields, taking care not to split on the comma if it is within quotes. Nor will it handle tohe given data correctly (tip: there's a telephone number which is not part of the address). Do try to read what other people have written more carefully, especially if it has been explicitly pointed out to you.

      As for your other remark, this is an area where it's unwise to use regular expressions because it's rather complicated to come up with a robust solution (take a look at Mastering Regular Expressions, it handles this subject in some detail) and there are perfectly well-developed solutions for parsing CSV files in CPAN, which have already been pointed out by others.


      Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
        Well, the OP didn't in fact ask for that, although I can agree now that they probably should have done.

        -M

        Free your mind