我有这段文字:
text = '"Friend", "One, Two, Three", "something else"'
我想将其转换为数组:
array = [
"Friend",
"One, Two, Three",
"something else"
]
我该如何在Ruby中做到这一点?简单split()
可能行不通,因为,
可能在某些元素内(如本例所示)。也许有一些图书馆吗?
String#scan
带有正则表达式:
text = '"Friend", "One, Two, Three", "something else"'
text.scan(/\"([,\ \w]+)\"/).flatten
#=> ["Friend", "One, Two, Three", "something else"]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句