Im trying to create a regexp for this query:
SELECT gruppo
FROM righe_conto_ready
WHERE regexp_replace(gruppo,'(\[{1})|(\].*?\[)|(\].*$)','','g') = '[U6][U53]'
LIMIT 10
This is an example of 'gruppo' column:
[U6] CAFFETTERIA [U43] THE E TISANE
Im currently using this query for testing:
SELECT regexp_replace(gruppo,'(\[{1})|(\].*?\[)|(\].*$)','','g') FROM ....
and it returns just U6
How can i change the regexp to remove everything outside brackets?
You can use
regexp_matches()
with the much simpler regular expression:When you are looking for multiple matches of some pattern,
regexp_matches()
seems more natural thanregexp_replace()
.You can also search for first two substrings in brackets (without the
g
flag the function yields no more than one row):