c#中 如何用正则表达式提取指定行? 例如 提取一个txt文档的第10行文本。
正则表达式该怎么写?

解决方案 »

  1.   

    Try:string reg = "(.*\\s){9}(.*\\s)([\\s\\S]*)";string res = Regex.Replace(orignal, reg, string.Format("{0}$1{2}", "", "")); 
      

  2.   

    Text = System.Text.RegularExpressions.Regex.Match(
        textBox1.Text, "([^\r\n]*\r\n){9}((?<Line>[^\r\n]*)(\r\n)*)"
        ).Result("${Line}");
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Text.RegularExpressions;namespace ConsoleApplication76
    {
        class Program
        {
            static void Main(string[] args)
            {
                int n = 10;
                //string text = new StreamReader("C:\\a.txt").ReadToEnd();
                string text =
    @"1
    24
    5asdf
    6
    7
    8
    9
    10
    ";
                Match m = Regex.Match(text, ".+");
                while (--n > 0)
                    m = m.NextMatch();
                Console.WriteLine(m);
                Console.Read();
            }
        }
    }
      

  4.   

    读一行而已嘛 用不着正则~~用10次ReadLine()就行了,绝对比正则效率高~~~