ruby - What is the best way to delimit a csv files thats contain commas and double quotes? -
lets have following string , want below output without requiring csv.
this, "what need", to, do, "i, want, this", to, work need i, want, work
this problem classic case of technique explained in question "regex-match pattern, excluding..."
we can solve beautifully-simple regex:
"([^"]+)"|[^, ]+ the left side of alternation | matches complete "quotes" , captures contents group1. right side matches characters neither commas nor spaces, , know right ones because not matched expression on left.
option 2: allowing multiple words
in input, tokens single words, if want regex work my cat scratches, "what need", dog barks, use this:
"([^"]+)"|[^, ]+(?:[ ]*[^, ]+)* the difference addition of (?:[ ]*[^, ]+)* optionally adds spaces + characters, 0 or more times.
this program shows how use regex (see results @ bottom of online demo):
subject = 'this, "what need", to, do, "i, want, this", to, work' regex = /"([^"]+)"|[^, ]+/ # put group 1 captures in array mymatches = [] subject.scan(regex) {|m| $1.nil? ? mymatches << $& : mymatches << $1 } mymatches.each { |x| puts x } output
this need i, want, work reference
Comments
Post a Comment