Given an array string in Ruby with some elements in quotation marks that contain commas:
my_string.inspect # => "\"hey, you\", 21"
How can I get an array:
["hey, you", " 21"]
The standard Ruby CSV library .parse_csv does just that.
.parse_csv
require 'csv' "\"hey, you\", 21".parse_csv # => ["hey, you", " 21"]
Yes, using CSV :: parse_line is the way to go here, but you can 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 prefer to have "21" rather than " 21" , just delete \s .
"21"
" 21"
\s
Source: https://habr.com/ru/post/1234635/More articles:Configure VSCode to use console emulator instead of command line - visual-studio-codeI am trying to use TFS2015 to publish a solution for a website like VS2015 using Publish Web App - asp.net-coreHow to open pdf file from Vim using default pdf application? - vimhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1234633/spring-stomp-how-to-avoid-multiple-subscriptions-to-same-destination-by-same-user&usg=ALkJrhjZbbsbbFHaxJGNhkGl0Gyn_jTNzAJQuery UI Autocomplete custom display, how to focus child children instead of first-level children? - javascriptSwift - Clear TableView - swift2Can I check type for union type in typescript? - typescriptThe Youtube channel list brandingSettings is empty with the name forUsername but not id - youtube-apiPython __init__.py and classes - pythonYouTube API - no channel branding settings returned for requests by username - youtubeAll Articles