how to extract only the words from this string: "[\"\\\"blink\\\",\", \"\\\"freakonomics\\\",\", \"\\\"outliers\\\"\"]" in ruby

To extract only the words from the given string in Ruby, you can use regular expressions and the scan method. Here's how you can do it:

main.rb
string = "[\"\\\"blink\\\",\", \"\\\"freakonomics\\\",\", \"\\\"outliers\\\"\"]"
words = string.scan(/\w+/)
puts words
119 chars
4 lines

The scan method accepts a regular expression as an argument and returns an array of all matching substrings. In this case, we are using the \w+ regular expression which matches one or more word characters (letters, digits, or underscores).

Running the above code will output:

main.rb
["blink", "freakonomics", "outliers"]
38 chars
2 lines

This extracts only the words from the given string and stores them in the words array.

related categories

gistlibby LogSnag