Basically: take the pattern (containing 3 groups), replace instr with second group from pattern.
private string MyReplace(string inStr, string leaveStr)
{
string pattern = @"(.*?)(" + leaveStr + ")(.*)";
string repl = @"*$2*";
Regex rgx = new Regex(pattern);
return rgx.Replace(inStr, repl);
}
string x = MyReplace(@"\textbf\{atext\}", "atext");
x = MyReplace(@"\textbf\{1\}", "1");
full string – group zero ($0)
(.*?) – first group ($1)
(atext) – second group ($2)
(.*) – third group ($3)
4
solved Replacing a substring with given form with another in C#