Reputation: 91
I have these characters in a table:
LEASE THIRD-CCP-MANAGER (AAAA)
THE MANAGEMENT OF A THIRD PARTY (BBBB / AAAA)
When I extract the information:
AAAA
BBBB/AAAA
That is, I have to look for the pattern and extract what is inside the parenthesis.
I'm trying to use the REGEXP_SUBSTR function.
In amazon redshift, how do I extract the characters in parentheses?
thanks
Upvotes: 1
Views: 3386
Reputation: 269284
Whoa, that was hard!
Here is the syntax to use:
SELECT REGEXP_SUBSTR('One (Two) Three', '[(](.*)[)]', 1, 1, 'e')
This will return: Two
It appears that escaping brackets with \(
doesn't work, but putting them in [(]
does work. The 'e'
at the end will "Extract a substring using a subexpression".
Upvotes: 4
Reputation: 86706
You’re probably struggling with ()
meaning something in regular expressions (lookup “back references”)
To tell regex that you just mean the characters (
and )
without their special meaning, “escape” them using \
regexp_substr(yourtable.yourcolumn,'\(.*\)')
Upvotes: 0
Reputation: 32003
use position
for finding the index of parenthesis (
and then substring
select
substring(position('(' in 'LEASE THIRD-CCP-MANAGER (AAAA)'),position(')' in 'LEASE THIRD-CCP-MANAGER (AAAA)'))
or you can use split_part
split_part('LEASE THIRD-CCP-MANAGER (AAAA)','(',2)
Upvotes: 0