C# Regex - How to remove multiple paired parentheses from string

前端 未结 4 1985
梦如初夏
梦如初夏 2020-12-09 19:17

I am trying to figure out how to use C# regular expressions to remove all instances paired parentheses from a string. The parentheses and all text between them should be rem

4条回答
  •  庸人自扰
    2020-12-09 19:56

    Fortunately, .NET allows recursion in regexes (see Balancing Group Definitions):

    Regex regexObj = new Regex(
        @"\(              # Match an opening parenthesis.
          (?>             # Then either match (possessively):
           [^()]+         #  any characters except parentheses
          |               # or
           \( (?)  #  an opening paren (and increase the parens counter)
          |               # or
           \) (?<-Depth>) #  a closing paren (and decrease the parens counter).
          )*              # Repeat as needed.
         (?(Depth)(?!))   # Assert that the parens counter is at zero.
         \)               # Then match a closing parenthesis.",
        RegexOptions.IgnorePatternWhitespace);
    

    In case anyone is wondering: The "parens counter" may never go below zero ( will fail otherwise), so even if the parentheses are "balanced" but aren't correctly matched (like ()))((()), this regex will not be fooled.

    For more information, read Jeffrey Friedl's excellent book "Mastering Regular Expressions" (p. 436)

提交回复
热议问题