有一大串字符,放在一个文本中
想找到一行中含有"session"的行,就找前面行中,离这一行最近的含有"interface"的字符串,写入另外一个文本文件中,如何做?

解决方案 »

  1.   

    從前向後逐行讀取,寫入數組,并判斷是否包含session,包含則停止讀取并向前遍曆數組,找到第一個包含interface的成員
      

  2.   

    具体的文本?
    貌似Substring(),IndexOf()应该就可以解决问题了..
      

  3.   

    类似于这样

    interface gigabitEthernet 2/1.100
     vlan id 100
     ip description ydl_lan_3550_1
     ip address 61.177.93.225 255.255.255.252

    interface gigabitEthernet 2/1.159

    interface gigabitEthernet 2/1.167
     vlan id 167
     ip unnumbered loopback 6

    interface gigabitEthernet 2/1.1000
     vlan id 1000
     pppoe
     pppoe sessions 5
     pppoe auto-configure
     pppoe profile any multicast

    interface gigabitEthernet 2/1.11502
     svlan id 115 2
     svlan ethertype 8100
     pppoe
     pppoe sessions 1
     pppoe auto-configure
     pppoe profile any pppoe

    interface gigabitEthernet 2/1.11503
     svlan id 115 3
     svlan ethertype 8100
     pppoe
     pppoe sessions 1
     pppoe auto-configure
     pppoe profile any pppoe
      

  4.   

    string input=".....";//你自己写读文本的代码
    Regex reg=new Regex(@"\s?(interface\s[^\n]+)(?:(?!\sinterface\s|\ssessions\s).)*\ssessions\s");
    StringBuilder sb=new StringBuild();
    foreach(Match m in reg.Matches(input)){
        sb.AppendLine(m.Groups[1].Value);
    }
    string output=sb.ToString();//结果