How can I split by fields below (this is sql format)? Previously I use to just split by ', ' the problem it fails if that character sequence is within the quotes. Unfortunately I also can't split by quotes because the numbers don't have any.
mystring = "(1, 'data, ', 'data_two, ', 'Test 34', '', 'gb', 1, '1')"
mystring.split(', ')
I need to get
'1'
'data'
'data_two, '
'Test 34'
''
'gb'
'1'
'1'
If you strip the parens, you can coerce it into being parseable using CSV
with options.
CSV.parse_line(mystring[1..-2], {:col_sep=>", ", :quote_char=>"'"})
=> ["1", "data, ", "data_two, ", "Test 34", "", "gb", "1", "1"]
mystring.split(/,/).map{|x| x.gsub(/[()\']/,'').strip}
=> ["1", "data", "", "data_two", "", "Test 34", "", "gb", "1", "1"]
This may be helpful:
irb(main):001:0> mystring
=> "(1, 'data, ', 'data_two, ', 'Test 34', '', 'gb', 1, '1')"
irb(main):002:0> mystring.scan(/(?:'(?:\\.|[^'])*'|[^,' ])+/)
=> ["(1", "'data, '", "'data_two, '", "'Test 34'", "''", "'gb'", "1", "'1')"]
Or, if you need quotes and braces to be removed, then:
irb(main):003:0> mystring.scan(/([^', ()]+)|'([^']*)'/).flatten.compact
=> ["1", "data, ", "data_two, ", "Test 34", "", "gb", "1", "1"]
This will match any non quote/brace/comma/space characted word, or anything inside of single 'quotes'. (Note, that escape sequence ( \\x
) is not accounted in second example unlike first one. But it may be that you don't need such complications.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.