.NET regex exclusive feature; looks like `(?
Questions tagged [balancing-groups]
24 questions
1
vote
1 answer
Regular Expressions Balancing Group
I am trying to match balancing braces ({}) in strings. For example, I want to balance the following:
if (a == 2)
{
doSomething();
{
int x = 10;
}
}
// this is a comment
while (a <= b){
print(a++);
}
I came up with this regex from…
user342552
1
vote
1 answer
Possible to match at one position of regex but not another (e.g. positional XOR)?
I am looking to create several sub-expressions in a larger regular expression, where each subexpression matches something at one place in the input or another place, but not in both places, preferably using the same named group per "area of…

Kit
- 20,354
- 4
- 60
- 103
1
vote
0 answers
Regex for balanced and unbalanced strings
I need to match strings that can be between parentheses {}. The string can also contain more types of parentheses. It can be balanced and unbalanced, too.
For example: {Title} {Size{()} {Color({[]})} {Test{} {Color{}}
So, I need to remove actually…

Orsi
- 545
- 2
- 10
- 27
1
vote
1 answer
Balancing Groups in .Net Regex not nesting correctly
I am trying to wrap my head around a bug I am finding with the .Net balancing groups regex.
I am trying to match !{} as an opening/closing combination.
Current Regex -> !{[^!{}]*(((?!{)[^!{}]*)+((?})[^!{}]*)+)*(?(Open)(?!))}
this…

Mark Redfern
- 357
- 1
- 3
- 24
1
vote
1 answer
Avoid regex balancing group to move out of parentheses
I'm using the following regex to match the contents of any datascript script referencing a specific UDF:
\[?shared3\]?\.\[?stringsum\]?(((?'Open'\()[^()]*)+((?'Close-Open'\))[^()]*)+)*
it matches any instance of:
Shared3.StringSum(

PaulVrugt
- 1,682
- 2
- 17
- 40
1
vote
1 answer
Regex to match escapable strings?
I wrote this regex to match strings:
(?>(?
""|').*?(?) i.e., some text enclosed in quotes. It also supports escaping, so it will match "hello\"world" in its entirety without stopping at the first quote, like I want. But I forgot…

mpen
- 272,448
- 266
- 850
- 1,236
1
vote
1 answer
Regular expression that uses balancing groups
I have a basic text template engine that uses a syntax like this:
foo bar
%IF MY_VAR
some text
%IF OTHER_VAR
some other text
%ENDIF
%ENDIF
bar foo
I have an issue with the regular expression that I am using to parse it whereby it is not…

nbevans
- 7,739
- 3
- 27
- 32
1
vote
4 answers
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…

Gabriel S.
- 1,347
- 11
- 31
0
votes
0 answers
Why does this attempt on using balancing groups in Regex fail?
For the string "1-2-3-4", I wanted to create a regex, that would give me the following matches, i.e. each matching pair of digits separated by 1 or more dashes:
"1-2"
"2-3"
"3-4"
with each digit in its own capture group.
First attempt (c#…

Frederik Struck-Schøning
- 12,981
- 8
- 59
- 68