Problem
(sorry, had problems, cutting it into smaller pieces):
I have the following one column table A
s
-------------------------------------------
(AAB) Some name1 1234 (XY*) Hello world 12
(BB) Just one 123
and a lookup table L
a1 | a2
-----+-----
XY* | XY
AAB | A2B
I want to :
- retrieve the parts in the brackets:
(AAB, XY*) - get the corresponding
a2entry from the lookup table:(A2B, XY) - retrieve the number part of the entries:
(1234, 12) - combine the results from 2 and 4 into an array:
(A2B1234, XY12)
The results would be
s
-------------------------------------------
{A2B1234, XY12}
{BB123}
What I tried
This is how far I got:
For 1:
SELECT array_to_string(regexp_matches(s, '\((.*?)\)', 'g'), '') as in_bracket FROM A;For 3:
SELECT array_to_string(regexp_matches(s, '(\d+)', 'g'), '') as numbers FROM A;
Here the struggle begins. How can I
- Replace the values (
in_bracket) with the lookup values on the fly? - Combine the results from the two select clauses into one array per row?
The data:
CREATE TABLE A (
s VARCHAR
);
INSERT INTO a VALUES
('(AAB) Some name1 1234 (BB) More text 99 (XY*) Hello world 12'),
('(BB) Just one 123');
CREATE TABLE L (
a1 VARCHAR(4),
a2 VARCHAR(4)
);
INSERT INTO L VALUES
('XY*', 'XY'),
('AAB', 'A2B');