Home > Mobile >  Remove everithing that it outside the brackets
Remove everithing that it outside the brackets

Time:11-04

I need your help.

I working with Oracle SQL and I need to remove everything that it outside the brackets. For example:

 - Stabilization loans, mortgage lending (StabL ML) => StabL ML
 - Refinanced loans and restructured loans (RefL RL) => RefL RL
 - Individual reserve (IR) => IR

I'm trying to use something like this but it doen't work

select regexp_replace(example, '\([A-Za-z ]\)', '') from dual;

I will be grateful for any help!

CodePudding user response:

Although you might consider using a pattern like .*\(|\).* with REGEXP_REPLACE, I'd rather you use

REGEXP_SUBSTR(example, '\(([^()]*)\)', 1, 1, 'i', 1)

See the DBfiddle.

Details:

  • \( - a ( char
  • ([^()]*) - Group 1: any zero or more chars other than parentheses
  • \) - a ) char.

1, 1, 'i', 1 make the regex engine search from the first char in the string only once (in a case insensitive way, though it is not quite important here) and the last 1 extracts Group 1 value.

CodePudding user response:

Your algorithm is partially correct. But you have to add the expression is " "

'\([A-Za-z ] \)'

Regex101

or

Regex101

'([A-Za-z]. )([(])([A-Za-z]. )([)])'

You can use the code as above with substitution "$3". But I think this code maybe more useful for you.

  • Related