Ruby中的字符串解析

问题描述 投票:0回答:1

我有一个字符串,需要在ruby中进行解析。

ex: a= '"a,b","c,d","e,f"'

我将Postgres用作数据库,并使用以下命令返回不同的输出。

example: a='"a,b","c,d","e,f"'
a.split('"')

original output: ["", "a,b", ",", "c,d", ",", "e,f"]
expected output: ["a,b" "c,d","e,f"]
ruby-on-rails ruby string string-parsing
1个回答
0
投票

您可以将String#split与正则表达式一起使用。

str = '"a,b","c,d","e,f"'
  #=> "\"a,b\",\"c,d\",\"e,f\"" 

arr = str.split(/(?<="),(?=")/)
  #=> ["\"a,b\"", "\"c,d\"", "\"e,f\""] 
puts arr
"a,b"
"c,d"
"e,f"
© www.soinside.com 2019 - 2024. All rights reserved.