我需要從文本中洗掉旁邊帶有分隔符的單詞。問題是程式只洗掉單詞后的 1 個分隔符,但其中有很多。任何建議如何洗掉其他分隔符?另外,我需要確保這個詞沒有與其他字母相連。例如(如果單詞是 fHouse 或 Housef,則不應將其洗掉)
目前我有:
public static void Process(string fin, string fout)
{
using (var foutv = File.CreateText(fout)) //fout - OutPut.txt
{
using (StreamReader reader = new StreamReader(fin)) // fin - InPut.txt
{
string line;
while ((line = reader.ReadLine()) != null)
{
string[] WordsToRemove = { "Home", "House", "Room" };
char[] seperators = {';', ' ', '.', ',', '!', '?', ':'};
foreach(string word in WordsToRemove)
{
foreach (char seperator in seperators)
{
line = line.Replace(word seperator, string.Empty);
}
}
foutv.WriteLine(line);
}
}
}
}
我有 :
fhgkHouse!House!Dog;;;!!Inside!C!Room!Home!House!Room;;;;;;;;;;!Table!London!Computer!Room;..;
結果我得到:
fhgkDog;;;!!Inside!C!;;;;;;;;;!Table!London!Computer!..;
結果應該是:
fhgkHouse!Dog;;;!!Inside!C!Table!London!Computer!
uj5u.com熱心網友回復:
試試這個正則運算式:\b(Home|House|Room)(!|;)*\b|; \.\.;
請參閱: https ://regex101.com/r/LUsyM8/1
在那里,我用空白或空字串替換單詞和特殊字符。
我猜它會產生相同的預期結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/537942.html
標籤:C#
上一篇:如何在流程時呼叫活動指示器
下一篇:從另一種形式到達圖表