using (StreamReader sr = new StreamReader(@"D:\1.txt")) { string s = sr.ReadToEnd(); //开始字符串 string s1 = textBox1.Text; //结束字符串 string s2 = textBox2.Text; // Regex rg = new Regex("(?<=(" + s1 + "))[.\\s\\S]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline); Regex rg = new Regex("(?<=(" + s1 + "))[^("+s1+")]*?(?=(" + s2 + "))", RegexOptions.Multiline | RegexOptions.Singleline); // 避免s1xxxs1xxxs2这类bug foreach (var m in rg.Matches(s)) { StreamWriter sw = new StreamWriter(@"D:\2.txt", true); sw.WriteLine(m.ToString()); sw.Close(); }