我有一个字符串,需要在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"]
您可以将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"