regex - .split and regular expression in Ruby -
i want split string (',') ignore ',' if inside quotes. example
" 2,2,4,'hello', 'world', 'hi, there' "
i want split 'hi, there' not split 2 different array elements in ruby. how can that? use regex?
edit: if use this, (from link possible dublicate)
values = values.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)', -1)
my string split correctly, can not use .delete_at() method on array. before do:
values.delete_at(20)
very well. taking inspiration this answer, regular expression looking is:
values.split(/,(?=(?:[^']*'[^']*')*[^']*$)/)
this not work if have escaped quotes, example (e.g. "'o\'reilly\'s car'"
).
however, looks bit xy problem. if want parse csv, seems, , if compliant csv, use csv.parse
or csv.parse_line
. not, due spaces between column separators. using standard formats , standard parsers is, if possible, preferable home-grown solutions.
Comments
Post a Comment