我想寫一個程序,可以替換文本並替換正則表達式文本。 所以我有正則表達式替換麻煩一個part..I'm真正的菜鳥:)c#正則表達式替換問題
private void button2_Click(object sender, EventArgs e)
{
if (File.Exists(textBox1.Text))
{
//這是常規的更換:
if (checkBox1.Checked == false)
{
StreamReader sr = new StreamReader(textBox1.Text);
StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
string cur = "";
do
{
cur = sr.ReadLine();
cur = cur.Replace(textBox2.Text, textBox3.Text);
sw.WriteLine(cur);
}
while (!sr.EndOfStream);
sw.Close();
sr.Close();
MessageBox.Show("Finished, the new file is in the same directory as the old one");
}
//這是正則表達式替換:
if (checkBox1.Checked == true)
{
System.Text.RegularExpressions.Regex g = new Regex(@textBox2.Text);
using (StreamReader r = new StreamReader(textBox1.Text))
{
StreamReader sr = new StreamReader(textBox1.Text);
StreamWriter sw = new StreamWriter(textBox1.Text.Replace(".", "_new."));
string cur = "";
do
{
cur = sr.ReadLine();
cur = cur.Replace(textBox2.Text, textBox3.Text);
sw.WriteLine(cur);
}
while (!sr.EndOfStream);
sw.Close();
sr.Close();
}
MessageBox.Show("Finished, the new file is in the same directory as the old one");
}
button2.Enabled = false;
}
if (File.Exists(textBox1.Text) == false)
{
MessageBox.Show("Please select a file and try again.");
}
}
這裏沒有問題。請具體說明問題所在。如果您收到例外情況,請提供詳細信息以及投放位置。 – Jay 2011-12-30 14:24:41
據我所見,除了實例化你的正則表達式之外,你沒有做任何事... – canon 2011-12-30 14:28:17
問題是什麼?我很想回應,因爲我認爲我看到了這個問題,但我會表現出剋制......沒有不好的非問題的回報...... – 2011-12-30 14:29:42