Ahkshaey Ravi Ahkshaey Ravi - 7 months ago 31
Ruby Question

remove special characters in an array in ruby

I have an array as shown below:

[
[
"[\"\", \"Mrs. Brain Bauch\", \"Vernice Ledner\"]",
"[\"\", \"Robb Ratke\", \"Amaya Jakubowski\"]",
"[\"\", \"Lindsey Cremin III\", \"Harvey Fisher\"]",
"[\"\", \"Daniela Schneider\", \"Benny Schumm\"]"
]
]


How can I convert this into the array structure shown below:

[
[
["Mrs. Brain Bauch", "Vernice Ledner"],
["Robb Ratke", "Amaya Jakubowski"],
["Lindsey Cremin III", "Harvey Fisher"],
["Daniela Schneider", "Benny Schumm"]
]
]

Answer

If arr is your array:

r = /
    (?<=\")    # match `\"` in a positive lookbehind
    [A-Z]      # match a capital letter
    [a-z\.\s]+ # match a letter, period or space one or more times
   /ix         # case-insenitive (i) and free-spacing (x) regex definition  modes

[arr.first.map { |s| s.scan r }]
  #=> [[["Mrs. Brain Bauch", "Vernice Ledner"],
  #     ["Robb Ratke", "Amaya Jakubowski"],
  #     ["Lindsey Cremin III", "Harvey Fisher"],
  #     ["Daniela Schneider", "Benny Schumm"]]] 
Comments