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

前端 未结 4 1981
梦如初夏
梦如初夏 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:48

    How about this: Regex Replace seems to do the trick.

    string Remove(string s, char begin, char end)
    {
        Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
        return regex.Replace(s, string.Empty);
    }
    
    
    string s = "Hello (my name) is (brian)"
    s = Remove(s, '(', ')');
    

    Output would be:

    "Hello is"
    

提交回复
热议问题