I am trying to extract words from a column in a table only if the entire word is in uppercase letters (I am trying to find all acronyms in a column). I tried using the following code, but it gives me all capital letters in a string even if it is just the first letter of a word.
SELECT title, REGEXP_REPLACE(title, '[^A-Z]+', '', 'g') AS acronym
FROM table;
Here is my desired output:
title | acronym |
---|---|
I will leave ASAP | ASAP |
David James is LOL | LOL |
BTW I went home | BTW |
Please RSVP today | RSVP |
2
Answers
You could use this regular expression:
.*?
matches arbitrary letters is a non-greedy fashionm
matches the beginning of a word(
is the beginning of the part we are interested in[[:upper:]]*
are arbitrarily many uppercase characters)
is the end of that part we are interested inM
matches the end of a word.*
matches arbitrary characters1
references the part delimited by the parenthesesYou could try REGEXP_MATCHES function
See demo here