QGIS Field Calculator – How to Remove Text Inside Parentheses Using Regular Expressions

field-calculatorqgisregular expression

On the QGIS Field calculator, I am trying to remove from a string the text that is inside parentheses using regexp_replace.

While it is not a problem to remove the text from a string that contains only one parentheses group (using

regexp_replace('Steep (61° to 84°)','[(](.*)?[)]','')

it gives "Steep" as a result.

I am having some issues with extracting the text from a string where are present multiple parentheses, e.g.,

'Steep (61° to 84°), Vertical / Near Vertical (85° to 90°)'

From the above string, I would like to obtain

'Steep, Vertical / Near Vertical'

but if I try to use the same regex I obtain again only "steep", because for the system the closing parenthesis is the one at the end of the string.

Best Answer

You can expand your group to include the [(] and [)] by moving the inner parentheses outwards, like so:

'([(].*?[)])'

The expression becomes:

regexp_replace('Steep (61° to 84°), Vertical / Near Vertical (85° to 90°)','([(].*?[)])','')

enter image description here