D
Deanna
I'm trying to use Regex.Replace to replace a single word in a string,
but I have to pass in the string. It won't ever match, but if I hard-
code the word it works fine. Please help!!
string stringToAlter = "ab cd ef gh";
// --- Pattern entered as string works fine
string pat1 = @"\bcd\b";
string newStg1 = Regex.Replace(stringToAlter, @pat1, "xxx");
WriteLine("first try = " + newStg1);
// --- But passing in the word and concatenating doesn't!
string pat2 = @"\b" + "cd" + "\b";
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
WriteLine("second try = " + newStg1);
results:
first try = ab xxx ef gh
second try = ab cd ef gh
but I have to pass in the string. It won't ever match, but if I hard-
code the word it works fine. Please help!!
string stringToAlter = "ab cd ef gh";
// --- Pattern entered as string works fine
string pat1 = @"\bcd\b";
string newStg1 = Regex.Replace(stringToAlter, @pat1, "xxx");
WriteLine("first try = " + newStg1);
// --- But passing in the word and concatenating doesn't!
string pat2 = @"\b" + "cd" + "\b";
newStg1 = Regex.Replace(stringToAlter, @pat2, "xxx");
WriteLine("second try = " + newStg1);
results:
first try = ab xxx ef gh
second try = ab cd ef gh