Given a string of an array in Ruby with some items in quotes that contain commas:
my_string.inspect
# => "\"hey, you\", 21"
How can I get an array of:
["hey, you", " 21"]
Answer: Use the split() Method You can use the JavaScript split() method to split a string using a specific separator such as comma ( , ), space, etc. If separator is an empty string, the string is converted to an array of characters.
How do I split a string based on space but take quoted Substrings as one word? \S* - followed by zero or more non-space characters.
Question marks and exclamation marks go inside the quotation marks when they are part of the original quotation. For split quotations, it's also necessary to add a comma after the first part of the quotation and after the narrative element (just like you would with a declarative quotation).
The Ruby standard CSV library's .parse_csv
, does exactly this.
require 'csv'
"\"hey, you\", 21".parse_csv
# => ["hey, you", " 21"]
Yes, using CSV::parse_line or String#parse_csv
, which require 'csv'
adds to String
's instance methods) is the way to go here, but you could also do it with a regex:
r = /
(?: # Begin non-capture group
(?<=\") # Match a double-quote in a positive lookbehined
.+? # Match one or more characters lazily
(?=\") # Match a double quote in a positive lookahead.
) # End non-capture group
| # Or
\s\d+ # Match a whitespace character followed by one or more digits
/x # Extended mode
str = "\"hey, you\", 21"
str.scan(r)
#=> ["hey, you", " 21"]
If you'd prefer to have "21"
rather than " 21"
, just remove \s
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With