Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string into an array by comma, unless comma is inside quotes

Tags:

ruby

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"] 
like image 459
steel Avatar asked Oct 27 '15 21:10

steel


People also ask

How do you split a string with commas in an array?

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?

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.

How do you split quotation marks?

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).


2 Answers

The Ruby standard CSV library's .parse_csv, does exactly this.

require 'csv'
"\"hey, you\", 21".parse_csv
# => ["hey, you", " 21"] 
like image 88
steel Avatar answered Nov 15 '22 06:11

steel


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.

like image 20
Cary Swoveland Avatar answered Nov 15 '22 07:11

Cary Swoveland