I would like to convert the following string into an array/nested array:
我想将以下字符串转换为数组/嵌套数组:
str = "[[this, is],[a, nested],[array]]"
newarray = # this is what I need help with!
newarray.inspect # => [['this','is'],['a','nested'],['array']]
5 个解决方案
#1
11
You'll get what you want with YAML.
你可以用YAML得到你想要的。
But there is a little problem with your string. YAML expects that there's a space behind the comma. So we need this
但是你的绳子有一个小问题。YAML期望逗号后面有空格。所以我们需要这个
str = "[[this, is], [a, nested], [array]]"
Code:
代码:
require 'yaml'
str = "[[this, is],[a, nested],[array]]"
### transform your string in a valid YAML-String
str.gsub!(/(\,)(\S)/, "\\1 \\2")
YAML::load(str)
# => [["this", "is"], ["a", "nested"], ["array"]]
#2
3
For a laugh:
一笑:
ary = eval("[[this, is],[a, nested],[array]]".gsub(/(\w+?)/, "'\\1'") )
=> [["this", "is"], ["a", "nested"], ["array"]]
Disclaimer: You definitely shouldn't do this as eval
is a terrible idea, but it is fast and has the useful side effect of throwing an exception if your nested arrays aren't valid
免责声明:您绝对不应该这样做,因为eval是个糟糕的想法,但是它速度很快,并且在嵌套数组无效的情况下具有抛出异常的有用副作用
#3
3
You could also treat it as almost-JSON. If the strings really are only letters, like in your example, then this will work:
您也可以将其视为几乎是json。如果字符串真的只是字母,比如在你的例子中,那么这个将会起作用:
JSON.parse(yourarray.gsub(/([a-z]+)/,'"\1"'))
If they could have arbitrary characters (other than [ ] , ), you'd need a little more:
如果他们可以有任意的字符(除了[],),你需要更多一点:
JSON.parse("[[this, is],[a, nested],[array]]".gsub(/, /,",").gsub(/([^\[\]\,]+)/,'"\1"'))
#4
0
Looks like a basic parsing task. Generally the approach you are going to want to take is to create a recursive function with the following general algorithm
看起来像是一个基本的解析任务。通常,您希望采用的方法是使用以下通用算法创建一个递归函数
base case (input doesn't begin with '[') return the input
recursive case:
split the input on ',' (you will need to find commas only at this level)
for each sub string call this method again with the sub string
return array containing the results from this recursive method
The only slighlty tricky part here is splitting the input on a single ','. You could write a separate function for this that would scan through the string and keep a count of the openbrackets - closedbrakets seen so far. Then only split on commas when the count is equal to zero.
这里唯一有点棘手的部分是把输入分成一个“,”。您可以为此编写一个单独的函数,该函数将扫描整个字符串,并保存到目前为止看到的open括弧- closedbrakets的计数。然后只有当计数等于0时才用逗号分开。
#5
0
Make a recursive function that takes the string and an integer offset, and "reads" out an array. That is, have it return an array or string (that it has read) and an integer offset pointing after the array. For example:
创建一个递归函数,接受字符串和一个整数偏移量,并“读取”一个数组。也就是说,让它返回一个数组或字符串(它已经读过),以及数组后面的整数偏移量。例如:
s = "[[this, is],[a, nested],[array]]"
yourFunc(s, 1) # returns ['this', 'is'] and 11.
yourFunc(s, 2) # returns 'this' and 6.
Then you can call it with another function that provides an offset of 0, and makes sure that the finishing offset is the length of the string.
然后可以使用另一个函数调用它,该函数提供0的偏移量,并确保结束偏移量是字符串的长度。
#1
11
You'll get what you want with YAML.
你可以用YAML得到你想要的。
But there is a little problem with your string. YAML expects that there's a space behind the comma. So we need this
但是你的绳子有一个小问题。YAML期望逗号后面有空格。所以我们需要这个
str = "[[this, is], [a, nested], [array]]"
Code:
代码:
require 'yaml'
str = "[[this, is],[a, nested],[array]]"
### transform your string in a valid YAML-String
str.gsub!(/(\,)(\S)/, "\\1 \\2")
YAML::load(str)
# => [["this", "is"], ["a", "nested"], ["array"]]
#2
3
For a laugh:
一笑:
ary = eval("[[this, is],[a, nested],[array]]".gsub(/(\w+?)/, "'\\1'") )
=> [["this", "is"], ["a", "nested"], ["array"]]
Disclaimer: You definitely shouldn't do this as eval
is a terrible idea, but it is fast and has the useful side effect of throwing an exception if your nested arrays aren't valid
免责声明:您绝对不应该这样做,因为eval是个糟糕的想法,但是它速度很快,并且在嵌套数组无效的情况下具有抛出异常的有用副作用
#3
3
You could also treat it as almost-JSON. If the strings really are only letters, like in your example, then this will work:
您也可以将其视为几乎是json。如果字符串真的只是字母,比如在你的例子中,那么这个将会起作用:
JSON.parse(yourarray.gsub(/([a-z]+)/,'"\1"'))
If they could have arbitrary characters (other than [ ] , ), you'd need a little more:
如果他们可以有任意的字符(除了[],),你需要更多一点:
JSON.parse("[[this, is],[a, nested],[array]]".gsub(/, /,",").gsub(/([^\[\]\,]+)/,'"\1"'))
#4
0
Looks like a basic parsing task. Generally the approach you are going to want to take is to create a recursive function with the following general algorithm
看起来像是一个基本的解析任务。通常,您希望采用的方法是使用以下通用算法创建一个递归函数
base case (input doesn't begin with '[') return the input
recursive case:
split the input on ',' (you will need to find commas only at this level)
for each sub string call this method again with the sub string
return array containing the results from this recursive method
The only slighlty tricky part here is splitting the input on a single ','. You could write a separate function for this that would scan through the string and keep a count of the openbrackets - closedbrakets seen so far. Then only split on commas when the count is equal to zero.
这里唯一有点棘手的部分是把输入分成一个“,”。您可以为此编写一个单独的函数,该函数将扫描整个字符串,并保存到目前为止看到的open括弧- closedbrakets的计数。然后只有当计数等于0时才用逗号分开。
#5
0
Make a recursive function that takes the string and an integer offset, and "reads" out an array. That is, have it return an array or string (that it has read) and an integer offset pointing after the array. For example:
创建一个递归函数,接受字符串和一个整数偏移量,并“读取”一个数组。也就是说,让它返回一个数组或字符串(它已经读过),以及数组后面的整数偏移量。例如:
s = "[[this, is],[a, nested],[array]]"
yourFunc(s, 1) # returns ['this', 'is'] and 11.
yourFunc(s, 2) # returns 'this' and 6.
Then you can call it with another function that provides an offset of 0, and makes sure that the finishing offset is the length of the string.
然后可以使用另一个函数调用它,该函数提供0的偏移量,并确保结束偏移量是字符串的长度。