I want to select a part of a string, but the problem is that the last character I want to select can have multiple occurrences.
I want to select 'Aggregate('
and end at the matching ')'
, any ()
in between can be ignored.
Examples:
string: Substr(Aggregate(SubQuery, SUM, [Model].Remark * [Object].Shortname + 10), 0, 1)
should return: Aggregate(SubQuery, SUM, [Model].Remark * [Object].Shortname + 10)string: Substr(Aggregate(SubQuery, SUM, [Model].Remark * ([Object].Shortname + 10)), 0, 1)
should return: Aggregate(SubQuery, SUM, [Model].Remark * ([Object].Shortname + 10))string: Substr(Aggregate(SubQuery, SUM, ([Model].Remark) * ([Object].Shortname + 10) ), 0, 1)
should return: Aggregate(SubQuery, SUM, ([Model].Remark) * ([Object].Shortname + 10) )
Is there a way to solve this with a regular expression? I'm using C#.
This is a little ugly, but you could use something like
Aggregate\(([^()]+|\(.*?\))*\)
It passes all your tests, but it can only match one level of nested parentheses.
This solution works with any level of nested parenthesis by using .NETs balancing groups:
(?x) # allow comments and ignore whitespace
Aggregate\(
(?:
[^()] # anything but ( and )
| (?<open> \( ) # ( -> open++
| (?<-open> \) ) # ) -> open--
)*
(?(open) (?!) ) # fail if open > 0
\)
I'm not sure how much the input varies but for the string examples in the question something as simple as this would work:
Aggregate\(.*\)(?=,)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With