Simple Ruby Regex for JSON string -


i have following simple json string:

{\"exclude\"=>[4, 5, 6, 10], \"include\"=>[]} 

and i'd extract each number in array following "exclude". in other words, i'd expect 0th match numbers, first match 4, second 5, , on. many thanks.

maybe not single neat regex might hope for:

s = '{\"exclude\"=>[4, 5, 6, 10], \"include\"=>[]}'  all_numbers = s[/\[[\d,\s]+\]/] # => "[4, 5, 6, 10]"  all_numbers.scan(/\d+/).map { |m| m.to_i } # => [4, 5, 6, 10]  # depends how trust regex grabs result all_numbers. eval(all_numbers) # => [4, 5, 6, 10]  # one-liner. s[/\[[\d,\s]+\]/].scan(/\d+/).map { |m| m.to_i } # => [4, 5, 6, 10] 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -