SupremeA SupremeA - 1 year ago 54
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


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

Answer Source

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)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download