-1

我的文件是

> A B C D unuse data  <begin> Addd as ss 1 My name is 2323 33 text 
> </end> 34344 no need

我的代码是

StringBuilder mSb = new StringBuilder();
           StreamReader sr = new StreamReader(@"E:\check.txt");
           String line;

           while (sr.ReadLine() != null)
           {

               mSb.AppendLine(sr.ReadLine());

           }
string matc = new Regex(@"(<begin>)(\n?.*)*</end>)?").Match(mSb.ToString()).ToString();

在这里它读取所有文件,但我只想直到我删除?从头到尾,我的程序崩溃了..

4

3 回答 3

0

我猜你正在寻找类似的东西

var text = File.ReadAllText(@"E:\check.txt");
var match = Regex.Match(text, @"(?s)<begin>.*?</end>").Value;
于 2012-12-13T17:02:10.907 回答
0

最终代码应如下所示:

var text = System.IO.ReadAllText(@"E:\check.txt");
var match = Regex.Match(text, @"<begin>(.*?)</end>", RegexOptions.SingleLine);
string matc = match.Success ? match.Groups[1].value : null;

希望这对您的追求有所帮助。

于 2012-12-13T17:09:23.537 回答
0

在尝试使用它之前检查匹配的值。也使用 File.ReadAllText 而不是处理流。

//var data = File.ReadAllText(@"C:\MyFile.txt")

string data = @"> A B C D unuse data  <begin> Addd as ss 1 My name is 2323 33 text
> </end> 34344 no need";

var mtch = Regex.Match(data, @"((<begin>)(.+?)(</end>))", RegexOptions.Multiline | RegexOptions.Singleline );

if (mtch.Success)
   Console.WriteLine (mtch.Groups[0].Value);
else
   Console.WriteLine ("Failure to match");

/* Outputs

<begin> Addd as ss 1 My name is 2323 33 text
> </end>

*/
于 2012-12-13T17:12:38.953 回答