balancing-groups

Converting PCRE recursive regex pattern to .NET balancing groups definition

PCRE has a feature called recursive pattern, which can be used to match nested subgroups. For example, consider the "grammar" Q -> \w | '[' A ';' Q* ','? Q* ']' | '<' A '>' A -> (Q | ',')* // to match ^A$. It can be done in PCRE with the pattern ^((?:,|(\w|\[(?1);(?2)*,?(?2)*\]|<(?1)>))*)$ (Example test case: http://www.ideone.com/...

Writing a regex to capture text between outer parenthesis

So I'm trying to a parse a file that has text in this format: outerkey = (innerkey = innervalue) It gets more complex. This is also legal in the file: outerkey = (innerkey = (twodeepkey = twodeepvalue)(twodeepkey2 = twodeepvalue2)) So I want to basically capture only the outerkey's text. I cannot guarantee that all of the text wi...

Backtracking a balancing group in a greedy repetition may cause imbalance?

As a generically brewed example for the purpose of this question, my intent is to match some number of a's, then an equal number of b's, plus one more b. Examine the two patterns exhibited in this snippet (also on ideone.com): var r1 = new Regex(@"(?xn) (?<A> a)+ (?<B-A> b)+ (?(A)(?!)) b "); var r2 = new Regex(@"(?xn) (?<...

Regexercise: factorials

This is an experimental new feature for StackOverlow: exercising your regex muscles by solving various classical problems. There is no one right answer, and in fact we should collect as many right answers as possible, as long as they offer educational value. All flavors accepted, but please document it clearly. As much as practical, p...

How can I retrieve the longest matches for substrings enclosed by "{{" and "}}" ?

I am trying to parse a wikitext file received through Wikipedia's API and the problem is that some of its templates (i.e. snippets enclosed in {{ and }}) are not automatically expanded into wikitext, so I have to manually look for them in the article source and replace them eventually. The question is, can I use regex in .NET to get the ...