1

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?

Tomalak
  • 306,836
  • 62
  • 485
  • 598

2 Answers2

3

"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:

Community
  • 1
  • 1
Tomalak
  • 306,836
  • 62
  • 485
  • 598
1

Just change your regex like below to match the brackets which ends with .

@"\((?:[^()]*\([^()]*\))*[^()]*\)\."

DEMO

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)
\)                       ')'
\.                       '.'
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229