Federal09
Federal09

Reputation: 649

Regex Replace String Between []

I have this code for replace the content between ()

 Regex yourRegex = new Regex(@"\(([^\)]+)\)");

            //Example strings
           string rep1 yourRegex.Replace("This is a (variable) string.", ""); //yields "This is a string."

Result is "This is a string."

but i have this string "This is a [variable] string". Now how to use Regex for same replace method?

Upvotes: 0

Views: 88

Answers (1)

Haney
Haney

Reputation: 34852

Use

Regex yourRegex = new Regex(@"\(.*\)|\[.*\]");

My version uses only matches, whereas yours uses groups which are a little more expensive and unnecessary for what you're trying to accomplish.

Upvotes: 1

Related Questions