2012-05-26 103 views
1

我正在嘗試編寫一個掃描環繞WiFi網絡並將信息轉儲到包含SSID和加密類型的數組中的程序。然後將SSID動態數組與靜態數組進行比較,嘗試將SSID匹配在一起,然後輸出結果。創建一個動態數組,並與靜態比較

我無法嘗試創建動態數組只使用正則表達式的SSID和加密。網絡轉儲的輸出是這樣的:

Interface name : Wireless Network Connection 
There are 8 networks currently visible. 

SSID 1 : TheChinaClub-5G 
    Network type   : Infrastructure 
    Authentication   : WPA2-Personal 
    Encryption    : CCMP 

我試着使用SSID與下列號碼作爲通配符鍵(但不知道語法)和冒號後獲取數據不包括空間。截至目前沒有任何工作,除了網絡轉儲。正則表達式似乎沒有找到任何東西。我一直在使用this post作爲正則表達式的模型。

下面是代碼我迄今:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Diagnostics; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace Rainbownetworks 
{ 
    public struct Networkarr 
    { 
     public string x, y; 

     public Networkarr(string SSID, string Encryption) 
     { 
      x = SSID; 
      y = Encryption; 
     } 
    } 

    class Program 
    { 

     static void Main(string[] args) 
     { 
      string[] StaticSSIDarr = { "network1", "network2", "network3" }; 
      string[] Networkarr = { }; 


      Process p = new Process(); 
      p.StartInfo.FileName = "netsh.exe"; 
      p.StartInfo.Arguments = "wlan show networks"; 
      p.StartInfo.UseShellExecute = false; 
      p.StartInfo.RedirectStandardOutput = true; 
      p.Start(); 
      string results = p.StandardOutput.ReadToEnd(); 

      string[] SSIDs = { "SSID *"}; 

      FindSSIDs(SSIDs, results); 

      Console.WriteLine(results); 
      Console.ReadLine(); 




     } 

     private static void FindSSIDs(IEnumerable<string> keywords, string source) 
     { 
      var found = new Dictionary<string, string>(10); 
      var keys = string.Join("|", keywords.ToArray()); 
      var matches = Regex.Matches(source, @"(?<key>" + keys + "):", 
            RegexOptions.IgnoreCase); 

      foreach (Match m in matches) 
      { 
       var key = m.Groups["key"].ToString(); 
       var start = m.Index + m.Length; 
       var nx = m.NextMatch(); 
       var end = (nx.Success ? nx.Index : source.Length); 
       found.Add(key, source.Substring(start, end - start)); 
      } 

      foreach (var n in found) 
      { 
       Networkarr newnetwork = new Networkarr(n.Key, n.Value); 
       Console.WriteLine("Key={0}, Value={1}", n.Key, n.Value); 
      } 
     } 
    } 


} 

回答

2

試試這個

try { 
    Regex regexObj = new Regex(@"(?<=SSID\s*\d+ :\s*)(?<value>\S+)"); 
    Match matchResults = regexObj.Match(subjectString); 
    while (matchResults.Success) { 
     for (int i = 1; i < matchResults.Groups.Count; i++) { 
      Group groupObj = matchResults.Groups[i]; 
      if (groupObj.Success) { 
       // matched text: groupObj.Value 
       // match start: groupObj.Index 
       // match length: groupObj.Length 
      } 
     } 
     matchResults = matchResults.NextMatch(); 
    } 
} catch (ArgumentException ex) { 
    // Syntax error in the regular expression 
} 

說明

@" 
(?<=   # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) 
    SSID   # Match the characters 「SSID」 literally 
    \s   # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
     *   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
    \d   # Match a single digit 0..9 
     +   # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
    \ :   # Match the characters 「 :」 literally 
    \s   # Match a single character that is a 「whitespace character」 (spaces, tabs, and line breaks) 
     *   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy) 
) 
(?<value> # Match the regular expression below and capture its match into backreference with name 「value」 
    \S   # Match a single character that is a 「non-whitespace character」 
     +   # Between one and unlimited times, as many times as possible, giving back as needed (greedy) 
) 
" 

希望這會有所幫助。

+0

這很好,唯一的問題是,如果SSID中有一個空格,字符串只顯示網絡的第一個字。 – Nick