C # 如何读取 TXT文件中的 指定字符?
例如 
这个是.txt文件的内容
1*客A-88888*##*1900/01/01*##*##*##
2*客A-77777*##*1900/01/01*##*##*##
3*客A-99000*##*1900/01/01*##*##*##
4*客A-88888*##*1900/01/01*##*##*##
5*客A-11111*##*1900/01/01*##*##*##
6*客A-12345*##*1900/01/01*##*##*##
7*北A-67890*##*1900/01/01*##*##*##
8*北A-098765*##*1900/01/01*##*##*##
9*北A-4321*##*1900/01/01*##*##*##
10*北A-22222*##*1900/01/01*##*##*##
11*天A-33333*##*1900/01/01*##*##*##
12*天A-66666*##*1900/01/01*##*##*##如何将“客A-88888”这些车牌号读取到指定的数组里,车牌号前边都是以*号开头,结尾也是以*号结尾的

解决方案 »

  1.   

    用正则表达式:@"(?<=\*).{2}-\d+(?=\*)"获取。
      

  2.   

    using System; 
    using System.IO;
    using System.Text;
    using System.Collections.Generic;
    using System.Text.RegularExpressions; class Program 

      static void Main() 
      { 
        using (StreamReader sr = new StreamReader("abc.txt", Encoding.GetEncoding("GB18030")))
        {
          string text = sr.ReadToEnd();
          // Console.WriteLine(text); 
          List<string> list = new List<string>();
          foreach (Match m in Regex.Matches(text, @"(?<=\*).{2}-\d+(?=\*)"))
          {
            list.Add(m.Value);
          }
          string[] array = list.ToArray(); // 这就是你要的车牌号数组。
          
          foreach (string s in array)
            Console.WriteLine(s); 
        }
      } 
    } /* 程序输出:
    客A-88888
    客A-77777
    客A-99000
    客A-88888
    客A-11111
    客A-12345
    北A-67890
    北A-098765
    北A-4321
    北A-22222
    天A-33333
    天A-66666
    */
      

  3.   

    tqi0321
    等 级:
    结帖率:0.00%不让自己后悔了
      

  4.   

    try...Regex reg = new Regex(@"(?<=\*)[^*-]+-[^*-]+(?=\*)");
    List<string> list = new List<string>();
    using (StreamReader sr = new StreamReader(@"G:\正则学习\test.txt", Encoding.Default))
    {
        MatchCollection mc = reg.Matches(sr.ReadToEnd());
        foreach (Match m in mc)
        {
            list.Add(m.Value);
        }
    }
    foreach (string s in list)
    {
        richTextBox2.Text += s + "\n";
    }
      

  5.   

    小弟 第一次初接触编程 第一次来到CSDN发贴 程序已经调试通过了 再这里感谢二楼楼主 二楼实在是太伟大了 还有一楼 五楼谢谢你们 
      

  6.   

     如何分析这一段代码呀?
    @"(?<=\*).{2}-\d+(?=\*)"