使用Regex正则表达式替换.txt文本文件中指定的词

 

 1         /// <summary>
 2         /// 替换文本文件中的词
 3         /// </summary>
 4         /// <param name="filePath"></param>
 5         /// <param name="oldWord"></param>
 6         /// <param name="newWord"></param>
 7         public static void ReplaceWordInTxt(string fliePath, string oldWord, string newWord)
 8         {
 9             string target = File.ReadAllText(fliePath);
10             var strFile = Regex.Replace(target, @"\b" + oldWord + @"\b", newWord);
11             File.WriteAllText(fliePath, strFile);
12         }

参考:https://stackoverflow.com/questions/13544425/how-can-i-replace-a-word-in-a-txt-file-in-c-sharp

posted @ 2022-11-19 11:56  SOLIDWORKS-API  阅读(194)  评论(0编辑  收藏  举报