.NET regex exclusive feature; looks like `(?
Questions tagged [balancing-groups]
24 questions
102
votes
2 answers
What are regular expression Balancing Groups?
I was just reading a question about how to get data inside double curly braces (this question), and then someone brought up balancing groups. I'm still not quite sure what they are and how to use them.
I read through Balancing Group Definition, but…

It'sNotALie.
- 22,289
- 12
- 68
- 103
22
votes
2 answers
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…

kennytm
- 510,854
- 105
- 1,084
- 1,005
14
votes
2 answers
Balancing groups in variable-length lookbehind
TL;DR: Using capturing (and in particular balancing groups) inside .NET's lookbehinds changes the obtained captures, although it shouldn't make a difference. What is it with .NET's lookbehinds that breaks the expected behavior?
I was trying to…

Martin Ender
- 43,427
- 11
- 90
- 130
5
votes
3 answers
How to make balancing group capturing?
Let's say I have this text input.
tes{}tR{R{abc}aD{mnoR{xyz}}}
I want to extract the ff output:
R{abc}
R{xyz}
D{mnoR{xyz}}
R{R{abc}aD{mnoR{xyz}}}
Currently, I can only extract what's inside the {}groups using balanced group approach as found…

sessionista
- 93
- 7
4
votes
2 answers
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…

polygenelubricants
- 376,812
- 128
- 561
- 623
4
votes
1 answer
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…

polygenelubricants
- 376,812
- 128
- 561
- 623
3
votes
3 answers
match regex with variable length look-behind of a word and variable length negative look-behind of another word?
I have a regular expression that captures a pattern A only if it the string contains a pattern B somewhere before A.
Let's say, for the sake of simplicity, that A is \b\d{3}\b (i.e. three digits) and B is the word "foo".
Therefore the Regex I have…

Edgar Hernandez
- 4,020
- 1
- 24
- 27
3
votes
2 answers
VBA: incompatible with .NET regex balancing groups?
I have been checking around as to whether I can use .NET regex balancing groups in an excel spreadsheet VBA function.
However, it appears that VBA is not compatible with, nor is it a part of .NET. For example, wikipedia bluntly states that "VBA is…

Jack BeNimble
- 35,733
- 41
- 130
- 213
2
votes
0 answers
.NET Regex Balancing Groups and syntax matching
I have some code containg null == myvar and want to change it myvar == null. Variants of code
if(null == myvar) > if(myvar == null)
if(null != myvar) > if(myvar != null)
if(null == myvar && someother) > if(myvar == null && someother)
if(null ==…

Anders
- 17,306
- 10
- 76
- 144
2
votes
2 answers
Regex and balancing groups
I'm stuck on a Regular expression:
I have a input string with numbers and one letter that can contain more numbers and letters within the string and between parenthesis:
Just a few examples
26U(35O40) will be read as 26 and (35 or…

Nekeniehl
- 1,633
- 18
- 35
2
votes
1 answer
using regular expression balancing groups to match nesting tags
i'm trying use regular expression balancing groups to match nesting tags looks like this:
some text ...
{list}
nesting loop content
{list}
{list}
{list}
bala ...
{/list}
{/list}
…

itsuki
- 31
- 1
- 4
2
votes
1 answer
RegEx Match VB.NET Select Case with no Case Else
I'm looking for a RegEx that will find Select Case Statements that have no Case Else in them.
Here's what I came up with so far
(?sm)^\s*Select Case.*(?

JoshKraker
- 373
- 1
- 5
- 15
2
votes
1 answer
Regex with balancing groups
I need to write regex that capture generic arguments (that also can be generic) of type name in special notation like this:
System.Action[Int32,Dictionary[Int32,Int32],Int32]
lets assume type name is [\w.]+ and parameter is [\w.,\[\]]+
so I need to…

Kovpaev Alexey
- 1,725
- 6
- 19
- 38
2
votes
2 answers
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…

Jason Thompson
- 4,643
- 5
- 50
- 74
2
votes
1 answer
c# regex with balancing groups not responding
I have following code:
void Main()
{
string template = @"
aaa
{begin iteration items}
bbbbbb
{begin iteration subitems}
ccccccc
{end iteration subitems}
ddddddddd
{begin iteration items}
hhhhhhhhhhhhhhhhh
{end iteration…

shibormot
- 1,638
- 2
- 12
- 23