SupremeA SupremeA - 4 months ago 13
Ruby Question

remove everything that is not capital letter words from a string Regex rails

I have a string

"WELLS FARGO DEALER SERVICES May 18 06:11 4137 Reference# 10982"


I want to remove everything so that I get

"WELLS FARGO DEALER SERVICES"


How can I do this with regex. I only want all cap words.

Answer

You can replace: /\W*\b\w*[^\WA-Z]\w*/ with the empty string.

or to deal with unicode letters: /\W*\b\w*[^\W\p{Lu}]\w*/u

To trim the end of the string, you can add |\W+\z at the end. (You can do the same kind of thing to trim the beginning)

demo

Comments