Anthony
Anthony

Reputation: 15967

Match on array and word

I have the following line from a log file:

"\\\"steps\\\"=\\u003e[5762.0, 0.0], \\\"minutes_sedentary\\\"=\\u003e[1390.0, 1440.0], \\\"minutes_lightly_active\\\"=\\u003e[23.0, 0.0], \\\"minutes_fairly_active\\\"=\\u003e[27.0, 0.0]}"

The end result of this is ideally a hash or array that looks like:

{
  "steps": [5762.0, 0.0],
  "minutes_sedentary": [1390.0, 1440.0],
  "minutes_lightly_active": [23.0, 0.0],
  "minutes_fairly_active": [27.0, 0.0]
}

I've got a really hacky solution that gsub's until I have all the words and then gsub's looking for the numbers but it's already missed a few edge cases (i.e. where decimal values can vary in length). I assume there is a better way but have been unsuccessful using String#match.

Upvotes: 0

Views: 34

Answers (1)

slugo
slugo

Reputation: 1039

This pattern captures in two groups the elements that you want.

(\w+).*?(\[.*?\])

You can test it here

Upvotes: 1

Related Questions