如何在C#中用正则表达式(正则表达式)替换单词?
示例:
string _test = "Test2 Test Test() Function(Test)";
_test = Regex.Replace(_test, regexVariable, "Boat");
会导致_test
的值为"Test2 Boat Boat() Function(Boat)"
答案 0 :(得分:2)
请参阅word boundary character,\b
。
Regex.Replace("Test2 Test Test() Function(Test)", @"\bTest\b", "Boat")
输出:
Test2 Boat Boat() Function(Boat)