how do i select only the letters in "\"five quacking zephyrs jolt my wax bed.\""? in ruby

To select only the letters from the given string ""five quacking zephyrs jolt my wax bed."", you can use the scan method in Ruby along with a regular expression to match the letters.

Here's an example code snippet to achieve that:

main.rb
str = "\"five quacking zephyrs jolt my wax bed.\""
letters = str.scan(/[a-zA-Z]/).join('')

puts letters
105 chars
5 lines

Output:

main.rb
fivequackingzephyrsjoltmywaxbed
32 chars
2 lines

In the code above, we use the scan method with the regular expression [a-zA-Z] to match all alphabetical letters (both lowercase and uppercase) in the string. We then use join to combine all the matched letters into a single string.

Note that the backslash \ before each letter is removed in the selected letters because it is treated as an escape character.

related categories

gistlibby LogSnag