使用正则表达式检索网络的BSSID

时间:2011-10-04 02:53:35

标签: c# .net regex

我运行以下命令来检索BSSID列表:

netsh wlan show networks mode=Bssid | findstr "SSID"

我得到了这个:

SSID 1 : John
    BSSID 1                 : b0:e7:54:f2:97:f9
SSID 2 : 2WIRE519
    BSSID 1                 : 00:1e:c7:fb:f5:89
SSID 3 : Home SCW
    BSSID 1                 : 00:1e:c7:fb:40:11
SSID 4 : CBV704W-AFE5
    BSSID 1                 : 00:1a:2b:57:2e:75
SSID 5 : neboi
    BSSID 1                 : 34:ef:44:76:e2:90

现在我想通过使用正则表达式和i存储单个BSSID 试试这个:

"^[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]$

......但是没有用。谁能帮我这个?这是一些示例代码:

string sPattern = "^[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]:[a-z0-9][a-z0-9]$";
            if (Regex.IsMatch(result, sPattern))
                Console.WriteLine("Pattern Found");
            else
                Console.WriteLine("Pattern Not Found");

2 个答案:

答案 0 :(得分:0)

尝试一下:

(?:[A-Fa-f0-9]{2}[:-]){5}(?:[A-Fa-f0-9]{2})

答案 1 :(得分:0)

static Regex MyRegex = new Regex(
      "([a-f0-9]{2}:){5}[a-f0-9]{2}",
    RegexOptions.Compiled);

static bool isBSSID(string line)
{
    return MyRegex.IsMatch(line);
}

void Main()
{
string lines = 
"SSID 1 : John\n"+
    "BSSID 1                 : b0:e7:54:f2:97:f9\n"+
"SSID 2 : 2WIRE519\n"+
    "BSSID 1                 : 00:1e:c7:fb:f5:89\n"+
"SSID 3 : Home SCW\n"+
    "BSSID 1                 : 00:1e:c7:fb:40:11\n"+
"SSID 4 : CBV704W-AFE5\n"+
    "BSSID 1                 : 00:1a:2b:57:2e:75\n"+
"SSID 5 : neboi\n"+
    "BSSID 1                 : 34:ef:44:76:e2:90";

    Console.WriteLine(isBSSID(lines)+"\n-----");    
    foreach (string line in lines.Split('\n'))
    {
        Console.WriteLine(isBSSID(line));       
    }
    //result: 
        //True
        //-----
        //False
        //True
        //False
        //True
        //False
        //True
        //False
        //True
        //False
        //True
}