0

I am trying to write a program that can replace text and replace also Regex text. So I am having trouble with the regex replace part..I'm a real noob :)

private void button2_Click(object sender, EventArgs e)
{
    if (File.Exists(textBox1.Text))
    {

//this is the regular replace:

        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");
        }

//this is the REGEX replace:

        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.");

    }
}
3
  • There is no question here. Please be specific about what the problem is. If you are getting exceptions, provide the details and where they are thrown. Commented Dec 30, 2011 at 14:24
  • 1
    As far as I can see you're not doing anything with your regex beyond instantiating it... Commented Dec 30, 2011 at 14:28
  • What's the question? I'm tempted to respond because I think I see the problem but I will show restraint...no reward for bad non-questions... Commented Dec 30, 2011 at 14:29

1 Answer 1

2

The Regex replace function can be found in the MSDN Regular Expression Replace documentation.

Use: Regex.Replace(input, pattern, replacement);

string inputFilename = textBox1.Text;
string outputFilename = inputFilename.Replace(".", "_new.");
string regexPattern = textBox2.Text;
string replaceText = textBox3.Text;

using (StreamWriter sw = new StreamWriter(outputFilename)))
{
    foreach (string line in File.ReadAllLines(inputFilename))
    {
        string newLine = Regex.Replace(line, regexPattern, replaceText);
        sw.WriteLine(newLine);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I just cant put it in the context. I want it to loop on all lines of a file
Added a more complete example now.
Optionally you could use File.ReadAllText and run the expression over the entire file in one shot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.