I want to split my string by (',') but ignore ',' if they are inside quotes. For example
" 2,2,4,'hello', 'world', 'hi, there' "
I want to split it so 'hi, there' will not be split into two different array elements in ruby. How can I do that? Probably use some regex?
EDIT: IF I use this, (from link to possible dublicate)
values = values.split(',(?=([^\"]*\"[^\"]*\")*[^\"]*$)', -1)
my string is split correctly, but now I can not use .delete_at() method on my array. Before I could do:
Best How To :
Very well. Taking inspiration from this answer, the regular expression you are looking for is:
This will not work if you have escaped quotes, for example (
e.g. "'O\'Reilly\'s car'").
However, this looks a bit like an XY problem. If you want to parse CSV, as it seems, and if this was a compliant CSV, you could use
CSV.parse_line. It is not, due to extra spaces between column separators. Using standard formats and standard parsers is, if possible, almost always preferable to home-grown solutions.