xecollons
xecollons

Reputation: 608

Regular Expression to get aggregates of a formula

For some calculation reasons, I have to take apart the aggregates of a formula. With aggregates, I mean "SUM", "AVG", "COUNT"...

The formula has different data that goes between brackets. Could be something like this:

([D23] +  SUM([D24]) + [D25]) + AVG([D28])[D47][D80] - [D75]

As you can see, the format of an aggregate is "AGREGATE([DATA])". Those aggregates can be SUM, AVG, COUNT, MAX, MIN... My goal is to get a regular expression that, with this formula, could have this output:

SUM([D24]) //One match
AVG([D28]) //Another match

I need to match the aggregates and the data that they reference. Maybe this is an easy pattern to find, but I'm new to RegEx, and I haven't been able to find it. I have tried some things, the closest that I have gotten is this pattern:

(SUM|AVG|COUNT|MAX|MIN)(\([^)(]*\))

that does what I want in terms of matches(the matches are correct, it seems), but it captures the match in two grups, like

SUM //One group
([D24]) //Another group

But, for my purposes, I need to capture everything in one group. Any hint for that would be appreciated.

Thanks.

Upvotes: 0

Views: 139

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174796

Make it as non capturing groups, so that it won't capture anything.

(?:SUM|AVG|COUNT|MAX|MIN)(?:\([^)(]*\))

DEMO

Upvotes: 1

Related Questions