I have strings that look like
{ABCDE}{F1}
{GHIJ}{K12}
I want to extract the text between the first curly brackets
ABCDE
GHIJ
I tried searching online but a lot of answers seem to be using functions or PL-SQL. Can someone please help me with this?
We can use REGEXP_SUBSTR
here:
SELECT regexp_substr('{ABCDE}{F1}', '\{([^}]+)\}', 1,1,NULL,1) AS output
FROM dual
This is a less commonly seen use of REGEXP_SUBSTR
, which uses a capture group, per this pattern:
\{([^}]+)\}
The sixth parameter says to return the first capture group.
This is old skool. I love regular expressions and can understand them but get into an awful lot of trouble when I try to produce them. A bit like Spanish (for me). So this is just SQL INSTR / SUBSTR / REPLACE. I'm not expecting any upvotes...
WITH test_data (raw_text)
AS
(SELECT '{ABCDE}{F1}' from dual UNION ALL
SELECT '{GHIJ}{K12}' from dual
)
SELECT
raw_text
,SUBSTR(raw_text,2,INSTR(raw_text,'}{')-2) first_string
,REPLACE(SUBSTR(raw_text,INSTR(raw_text,'}{')+2),'}') second_string
--these two from Tim's excellent answer
,regexp_substr(raw_text, '\{([^}]+)\}', 1,1,NULL,1)
,regexp_substr(raw_text, '\{([^}]+)\}', 1,2,NULL,1)
FROM
test_data
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With