我想讀其中有文件在C#中的話刪除#「#」附加到詞,我想從文字刪除此
輸入文件使用正則表達式
a, 00001740, 0.125, 0, able#1
a, 00001740, 0.125, 0, play#2
a, 00002098, 0, 0.75, unable#1
我在想這個下面有沒有#格式
輸出應該是這樣
a, 00001740, 0.125, 0, able
a, 00001740, 0 .125, 0, play
a, 00002098, 0, 0.75, unable
我寫了下面的代碼
TextWriter tw = new StreamWriter("D:\\output.txt");
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
StreamReader reader = new StreamReader("D:\\input.txt");
string line;
while ((line = reader.ReadLine()) != null)
{
Regex expression = new Regex(@"\b\w+(?=#\d*\b)");
var results = expression.Matches(reader.ToString())
foreach (Match match in results)
{
tw.Write(match);
}
tw.Write("\r\n");
}
tw.Close();
reader.Close();
}
textBox1.Text = "";
}
}
可能是你可以替換''#和刪除尾隨位.. – aspiring 2013-03-02 12:29:31