C# 创建动态数组并与静态数组进行比较
我正在尝试编写一个程序,扫描周围的WiFi网络,并将信息转储到包含SSID和加密类型的数组中。然后将SSID动态数组与尝试将SSID匹配在一起的静态数组进行比较,然后输出结果 我在尝试使用Regex创建动态数组时遇到了问题,只使用SSID和加密。网络转储的输出如下所示:C# 创建动态数组并与静态数组进行比较,c#,regex,arrays,C#,Regex,Arrays,我正在尝试编写一个程序,扫描周围的WiFi网络,并将信息转储到包含SSID和加密类型的数组中。然后将SSID动态数组与尝试将SSID匹配在一起的静态数组进行比较,然后输出结果 我在尝试使用Regex创建动态数组时遇到了问题,只使用SSID和加密。网络转储的输出如下所示: Interface name : Wireless Network Connection There are 8 networks currently visible. SSID 1 : TheChinaClub-5G
Interface name : Wireless Network Connection
There are 8 networks currently visible.
SSID 1 : TheChinaClub-5G
Network type : Infrastructure
Authentication : WPA2-Personal
Encryption : CCMP
我尝试使用SSID作为键,下面的数字作为通配符(但不知道语法),并获取冒号后面的数据,不包括空格。到目前为止,除了网络转储之外,其他功能都无法工作。正则表达式似乎找不到任何东西。我一直在使用正则表达式的模型
以下是迄今为止我掌握的代码:
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);
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统诊断;
使用系统文本;
使用System.Text.RegularExpressions;
名称空间彩虹网络
{
公共结构网络
{
公共字符串x,y;
公用网络ARR(字符串SSID、字符串加密)
{
x=SSID;
y=加密;
}
}
班级计划
{
静态void Main(字符串[]参数)
{
字符串[]StaticSSIDarr={“network1”、“network2”、“network3”};
字符串[]Networkarr={};
过程p=新过程();
p、 StartInfo.FileName=“netsh.exe”;
p、 StartInfo.Arguments=“wlan显示网络”;
p、 StartInfo.UseShellExecute=false;
p、 StartInfo.RedirectStandardOutput=true;
p、 Start();
字符串结果=p.StandardOutput.ReadToEnd();
字符串[]SSIDs={“SSID*”};
FinDSSID(SSID、结果);
控制台写入线(结果);
Console.ReadLine();
}
私有静态void FindSSIDs(IEnumerable关键字,字符串源)
{
发现的变量=新字典(10);
var keys=string.Join(“|”,关键字.ToArray());
var matches=Regex.matches(源,@“(?“+keys+”):”,
RegexOptions.IgnoreCase);
foreach(匹配中的匹配m)
{
var key=m.Groups[“key”].ToString();
var开始=m.指数+m.长度;
var nx=m.NextMatch();
var end=(nx.Success?nx.Index:source.Length);
Add(key,source.Substring(start,end-start));
}
foreach(发现的变量n)
{
Networkarr newnetwork=新Networkarr(n.Key,n.Value);
WriteLine(“Key={0},Value={1}”,n.Key,n.Value);
}
}
}
}
试试这个
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
}
试试看{
Regex regexObj=new Regex(@)(?这很有效唯一的问题是,如果SSID中有空格,字符串只显示网络的第一个字。
@"
(?<= # 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)
)
"