- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用正则表达式解析“ipconfig/all”的输出时遇到了一些麻烦。目前我正在使用 RegexBuddy 进行测试,但我想在 C#.NET 中使用正则表达式。
我的输出是:
Ethernet adapter Yes:
Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : MAC Bridge Miniport
Physical Address. . . . . . . . . : 02-1F-29-00-85-C9
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a%24(Preferred)
Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a7%24(Preferred)
Link-local IPv6 Address . . . . . : fe80::f980:c9c3:a574:37a8%24(Preferred)
IPv4 Address. . . . . . . . . . . : 10.0.0.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.0.0
IPv4 Address. . . . . . . . . . . : 172.16.0.1(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 172.16.0.254
DHCPv6 IAID . . . . . . . . . . . : 520228888
DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-17-1C-CC-CF-00-1F-29-00-85-C9
DNS Servers . . . . . . . . . . . : 192.162.100.15
192.162.100.16
NetBIOS over Tcpip. . . . . . . . : Enabled
到目前为止我写的正则表达式是:
([ -~]+):.+(?:Description\s)(?:\.|\s)+:\s([ -~]+).+(?:Physical Address)(?:\.|\s)+:\s([ -~]+).+(?:DHCP Enabled)(?:\.|\s)+:\s([ -~]+).+(?:(?:Link-local IPv6 Address)(?:\.|\s)+:\s([ -~]+).+Preferred.+)+
问题是我想将所有有用的字段作为组捕获,以便在 C# 中轻松获取它们,但出于某种原因 - 当我捕获多个“链路本地 IPv6 地址”字段时,它停止工作了。
我会很感激任何帮助,谢谢。
编辑:另一个问题是我从远程机器接收 ipconfig 数据(那里有一个我无法控制的非托管程序)——因此我不能使用 WMI 或类似的东西以另一种方式获取 ipconfig 信息。
最佳答案
but I want to use the regex in C#.NET.
为什么使用正则表达式?相信我,您不想使用正则表达式。一位智者曾经说过:
Some people, when confronted with a problem, think 'I know, I'll use regular expressions.' Now they have two problems.
现在让我陈述您的 2 个问题:
实际上,您可以直接使用 WMI 检索此信息,从而解决您原来的问题,再也不用考虑使用正则表达式了:
using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
using (var instances = mc.GetInstances())
{
foreach (ManagementObject instance in instances)
{
if (!(bool)instance["ipEnabled"])
{
continue;
}
Console.WriteLine("{0}, {1}, {2}", instance["Caption"], instance["ServiceName"], instance["MACAddress"]);
string[] ipAddresses = (string[])instance["IPAddress"];
string[] subnets = (string[])instance["IPSubnet"];
string[] gateways = (string[])instance["DefaultIPGateway"];
string domains = (string)instance["DNSDomain"];
string description = (string)instance["Description"];
bool dhcp = (bool)instance["DHCPEnabled"];
string[] dnses = (string[])instance["DNSServerSearchOrder"];
}
}
除此之外,您还可以使用 Mgmtclassgen.exe
为那些 WMI 类创建强类型包装器使您的代码更加安全的实用程序,您将能够摆脱魔法字符串。
关于c# - 解析窗口 'ipconfig/all' 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15184495/
我的 Adobe Flash 媒体服务器已成功安装在我的 Windows 中。但是每当我在命令中写入 ipconfig 时,它都会显示“ipconfig 不被识别为内部或外部命令” 最佳答案 在
我有以下正则表达式可以提取运行良好的键值对。我已经通过另一个系统从多台机器接收了这些数据,我更愿意使用我已经拥有的数据而不是使用 WMI 再次获取数据。 (^.*?)[^\w]+\:\s(.*?$|\
这里是一个编程初学者,所以我正在开发一个简单的程序,该程序使用用户的输入来命名一个文本文件,然后该程序将 ipconfig/all 信息存储在该文本文件中。我收到错误“命令行无法识别或不完整”。我知道
ipconfig 的输出在所有 Windows 计算机中都相同吗?。我想从 ipconfig 的输出中捕获 ip-address 、默认网关、子网掩码。 目前我正在这样做: String[] arr
我正在制作一个程序,您在客户端中键入 ifconfig(Linux) 或 ipconfig(Windows),然后它发送到服务器并在那里执行 shell。 执行正常,在服务器中我可以正确看到ipcon
我正在从 C# 打开命令提示符 Process.Start("cmd"); 当它打开时我需要自动编写 ipconfig 以便进程打开并找到工作站的 ip,我应该怎么做? 最佳答案 编辑 Proces
我在使用正则表达式解析“ipconfig/all”的输出时遇到了一些麻烦。目前我正在使用 RegexBuddy 进行测试,但我想在 C#.NET 中使用正则表达式。 我的输出是: Ethernet a
我正在使用 RenewDHCPLease() 来更新系统的 IP 地址。 RenewDHCPLease() 和 ipconfig/renew 之间的确切区别是什么? 哪个更适合在 Powershell
当与“特定于连接的 DNS 后缀”关联的 IP 地址更改时,我需要执行一些代码。我想可能不太好每隔 N 秒/分钟手动检查一次更改,并且有类似“监听器”的东西会在进行更改后立即执行我的代码。你知道吗?或
我对批处理文件很陌生,我正在尝试做一些相当高级的事情,并试图弄清楚如何识别和解析 IPCONFIG/all 中 DNS 服务器下的第二行。如果答案很高级,请您详细解释一下,我将不胜感激。这是我的代码:
我用这段代码来了解访问者(客户)的一些信息。它一直在我的 Xampp 虚拟服务器上运行,但我无法在我的主服务器(主机)上运行。我只看到一个空白页。 $info = system('ipconfig /
我想断开计算机与互联网的连接,我能想到的最好方法是从 cmd 运行 ipconfig/release。为此,我做到了 Process result = Runtime.getRuntime().ex
由于无法通过要求,我在开始作业时遇到了问题。 我们被指示在 Windows 命令行界面中执行 ipconfig/release 和 ipconfig/renew。我尝试了第一个,但它说: No ope
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
当我运行时,例如ipconfig使用运行命令(windows 键+R),一旦程序执行完毕,它会立即关闭命令行界面的实例,是否有任何通用参数可以添加到: ipconfig nslookup dir 等等
我是新来的,刚学python。我需要帮助才能在 Windows 中使用 python 获取我的网卡的正确 mac 地址。我尝试搜索,发现了这些: Python - Get mac address Ge
我正在 WinPE 环境中编写脚本,我想在其中找到我的 PXE 服务器并将我的 MAC 地址发送给它以检索配置脚本。 这是我必须处理的事情: 服务器将有多个网络适配器。只有一个连接到我的 PXE 服务
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
在我的主电脑(Win 7 64 位)上,ping 和 ipconfig 等基本 Windows 命令突然需要管理员权限。 如果我启动一个普通的命令行窗口,这些命令就再也找不到了(“X 不被识别为内部或
我尝试在实际设备上测试我的 Windows Phone 8 应用程序,但我需要计算机的 IP 地址才能执行此操作。当我在命令提示符中键入“ipconfig”时,它会显示两个不同的 IPv4 地址,但我
我是一名优秀的程序员,十分优秀!