C# regex with line breaks


Hello I have the following code

namespace ConsoleApplication2
    class Program
        static void Main(string[] args)

            string searchText = "find this text, and some other text";
            string replaceText = "replace with this text";

            String query = "%SystemDrive%";
            string str = Environment.ExpandEnvironmentVariables(query);
            string filePath = (str + "mytestfile.xml"); 

            StreamReader reader = new StreamReader( filePath );
            string content = reader.ReadToEnd();

            content = Regex.Replace( content, searchText, replaceText );

            StreamWriter writer = new StreamWriter( filePath );
            writer.Write( content );

the replace doesn't find the search text because it is on separate lines like

find this text,
and some other text.

How would I write the regex epression so that it will find the text.

Best Solution

Why are you trying to use regular expressions for a simple search and replace? Just use:


You may also need to add '\n' into your string to add a line break in order for the replace to match.

Try changing search text to:

string searchText = "find this text,\n" + 
                    "and some other text";