Since I don’t know what exactly you are trying to do, it’s hard to give reasonable advice, but it looks like your regular expression can be slightly improved.
Are you really trying to match strings like (foo) , [bar] and |baz| ? You do not need an alternator | inside character classes, so if you don't want to match the third example here, release | s.
Then, since you expect strings like (foo [bar] baz) , you need to separate the two kinds of parentheses, and you can also speed up your regex a bit:
@"^to |\\([^)]*\\)|\\[[^\\]]*\\]"
First, to checked at the beginning of the line, then a search for opening or brackets is performed, except for closing parsers / brackets and closing groove / brackets. This requires less return, so it may be a little faster.
You will not be able to handle nested parentheses / brackets of the same type ( (foo (bar) baz) ) with one regular expression because it is not regular - unless you run the replace regular expression operation several times, once for each nesting level. Thus, the above example will be deleted if you run the regular expression twice.
Tim pietzcker
source share