Reputation: 3048
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:
values.delete_at(20)
Upvotes: 0
Views: 226
Reputation: 110665
Here's a non-regex solution:
str = " 2,2,4,'hello', 'world', 'hi, there' "
first_quote_read = false
str.each_char.with_object (['']) do |c,a|
if c == ?, && !first_quote_read
a << ''
else
a[-1] << c
first_quote_read = !first_quote_read if c == ?'
end
end
#=> [" 2", "2", "4", "'hello'", " 'world'", " 'hi, there' "]
Upvotes: 1
Reputation: 198314
Very well. Taking inspiration from this answer, the regular expression you are looking for is:
values.split(/,(?=(?:[^']*'[^']*')*[^']*$)/)
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
or 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.
Upvotes: 2