How to do this replacement with regex? (problem with matching parentheses).

Hello.

I need to remove the occurrences of "!!sym(....)" from a string, keeping everything else.

But I need to keep what's inside the parentheses !!sym(....), which in turn can contain more parentheses, and the regex gets confusing.

For example, for this two strings:

"mutate(!!sym(myvars):= as.factor(!!sym(paste0(myvars,"_num", collapse=','))))"

"!!sym(paste0(x, '.Length')) / !!sym(paste0(x, '.Width'))"

I cannot just use:
"!!sym\((.+)\)"
nor
"!!sym\((.+?)\)" lazy
and keep the captured group \1
because it matches the wrong closing parentheses.
If inside the sym() there are three opening parenthesis I need to preserve also three closing parenthesis.

I've thought of something such as
((.*\(.*){m}(.*\).*){n}) but I don't know how to force m and n to be the same numbers of repetitions.

I think there is also a "recursive pattern" way, I don't know how it's done nor if there is something simpler.

It might be easier to remove the fixed part !!sym and leave whatever brackets there are alone entirely.
If ever evaluated, superfluous brackets dont cause problems for R to parse

Yes, :slight_smile:
But I would like to know how to remove the related parentheses too.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.