I want delete brakets that ends on a dot from string. I use regular expresion - @"\([^)]+\)\."
it works with string like this - some text (some text) some (text).
, after regular expression I have string - some text (some text) some
But this not work with string like that - some text (some text) some (text (text) some).
How to fix it?

- 332,285
- 67
- 532
- 628
2 Answers
"How to fix it?" Traditional answer: You can't. Regular expressions do not support rested constructs. This is true for most regex dialects out there.
The .NET regular expression engine however supports balancing groups. With them you can identify and handle nesting.
To handle a nested construct, you must define its opening and closing pattern, in your case those are the parentheses (
and )
, respectively.
- open:
(?<paren>\()
- close:
(?<-paren>\))
Think of this as a sort of counter named "paren" that counts up when it encounters (
and counts down when it encounters )
(internally, it's a bit different, but as a metaphor this is sufficient).
Now those two can be used to define the contents of a parenthesis, i.e
- either anything but a parenthesis:
[^()]*
- or the opening pattern
- or closing pattern from above
or, in one expression: (?:[^()]*|(?<paren>\()|(?<-paren>\)))+
The entire regex should fail when the counter is not zero at the end, i.e. the parentheses are not balanced. To make that happen, the (?(paren)(?!))
construct is used (that's a conditional, designed to fail when there is an unmatched paren
left).
Your finished expression looks like this (whitespace ignored)
\(
(?:
[^()]*
|(?<paren>\()
|(?<-paren>\))
)+
(?(paren)(?!))
\)\.$
See it live: http://regexhero.net/tester/?id=feb992a2-cc5d-497a-9d4a-a10317487e46
Recommended reading:
- What are regular expression Balancing Groups? here on SO (go read it, it's fantastic)
- MSDN: Grouping Constructs in Regular Expressions
- regular-expressions.info: Matching Nested Constructs with Balancing Groups
- Flagrant Badassery: Fun With .NET Regex Balancing Groups
- MSDN Blogs: Nested/Recursive Regex and .NET Balancing Groups
-
thks a lot, very intresting method – Nov 17 '14 at 13:03
Just change your regex like below to match the brackets which ends with .
@"\((?:[^()]*\([^()]*\))*[^()]*\)\."
Regular Expression:
\( '('
(?: group, but do not capture (0 or more
times):
[^()]* any character except: '(', ')' (0 or
more times)
\( '('
[^()]* any character except: '(', ')' (0 or
more times)
\) ')'
)* end of grouping
[^()]* any character except: '(', ')' (0 or more
times)
\) ')'
\. '.'

- 172,303
- 28
- 230
- 274