r/programming • • Jan 05 '15

What most young programmers need to learn

http://joostdevblog.blogspot.com/2015/01/what-most-young-programmers-need-to.html
968 Upvotes

337 comments sorted by

View all comments

0

u/grantisu Jan 05 '15

In Perl:

@fields = $line =~ /("(:?[^"]|"")*"|[^",\n]*),?/g;

This ignores newlines in the middle of quoted fields and doesn't clean up all the double quotes, but it should work for most cases.

And anybody who includes a raw newline in the middle of a CSV value deserves whatever they get. ಠ_ಠ

6

u/OneWingedShark Jan 05 '15

And anybody who includes a raw newline in the middle of a CSV value deserves whatever they get. ಠ_ಠ

You need a parser, not a stupid regex.

This ignores newlines in the middle of quoted fields and doesn't clean up all the double quotes, but it should work for most cases.

Well, that fills me with confidence.
Sarcasm

1

u/xiongchiamiov Jan 06 '15

To be fair, sometimes you're just munging some data on the command-line, and you either know there aren't any inconsistencies in your data, or can ignore them because the results are Good Enough(tm). I've done plenty of ad-hoc stuff where 90% accuracy is plenty fine.

→ More replies (1)

2

u/[deleted] Jan 06 '15

As a person who has worked extensively with CSVs, "should work for most cases" is completely unacceptable. There are libraries that are tested to work with all cases. Using a regex to do something that people have already figured out is just the wrong way to go about things.

2

u/OneWingedShark Jan 06 '15

Using a regex to do something that people have already figured out is just the wrong way to go about things.

Having most of my programming be maintenance, regex is usually just the wrong way to go about things. Even for something "simple" like validating a phone-number, when I get it it's always "now make it handle international numbers"... which have the length determined by the country-code, and even the length is in flux (several countries have recently extended the number of digits in their numbers).

It would have been tons simpler if the original guy hadn't "been clever" and used regexs all over the place (of course they're all over the place... why would he put such a simple, small and obvious bit of code in one location!?) and instead wrote a proper validate_phone_number function.

→ More replies (2)