首页 > 其他分享 >使用Regex正则表达式替换.txt文本文件中指定的词

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

时间:2022-11-19 12:13:44浏览次数:77  
标签:Regex string 文本文件 oldWord fliePath 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

标签:Regex,string,文本文件,oldWord,fliePath,txt
From: https://www.cnblogs.com/frankwu2014/p/16905814.html

相关文章