Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible? Matching the exact same number of opening & closing braces

Tags:

regex

qt

I'm no expert on Regex. I'm trying to create a regular expression that will match the exact same number of opening and closing braces, but I'm stumped on how to do it.

An example:

nothing: important, a b { c {{{ a another {{ nothing }} }}} }

or:

one { two {{ error, forgot ending brace }}

The problem is that I don't know how many braces I'm going to get beforehand. In the first example, I need to get the text { c {{{ a another {{ nothing }} }}} }. In the second I need to get {{ error, forgot ending brace }}.

Is it even possible to create a matching rule that will do this? (I'm using Qt Regex engine).

like image 380
Dave Berk Avatar asked Jan 19 '26 08:01

Dave Berk


1 Answers

This problem cannot be solved using regular grammar => it cannot be done by regular expression.

However - from Dirk Vollmar's comment - certain regex engines have extended functionality, e.g. the .NET one, which is capable of matching brackets using so called "balanced group definitions".

like image 107
Messa Avatar answered Jan 20 '26 22:01

Messa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!