regex - 使用 powershell 提取并打印 MAC 的前 3 个八位字节

标签 regex powershell mac-address

if($ping)
    {
        ( $mac | ? { $_ -match $ip } ) -match "([0-9A-F]{2}([:-][0-9A-F]{2}){5})" | out-null;
        if ( $matches ) {
            Select-String -Pattern '[0-9A-F]{2}-[0-9A-F]{2}-[0-9A-F]{2}' -Path $matches[0] |
            ForEach-Object {$_.Matches[0].Value}
        } else {
            "Not Found"
        }
    }

这是我目前正在使用的。不确定我在 PowerShell 中是否正确执行了此操作。

我正在尝试提取 MAC 的前 3 个 XX:XX:XX 以便我可以用它进行比较。我只是不确定比赛结束后的表现如何。

最佳答案

您可以使用删除来裁剪前 8 个字符:

$mac ='00:0a:95:9d:68:16' 

$mac.Remove(8, ($mac.Length -8))

输出:

00:0a:95

您还可以使用正则表达式来完成此操作:

[regex]::Match($mac, '^(.{8})').Groups[1].Value

关于regex - 使用 powershell 提取并打印 MAC 的前 3 个八位字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37465363/

相关文章:

android - 在 Kotlin 中使用正则表达式获取字符串中模式的索引

powershell - 如何通过管道正确展开数组?

sql-server - 使用Powershell从MSSQL数据库获取行数

ip - IP地址和MAC地址有什么区别?

python - 正则表达式,其中模式的一部分是可选的。

regex - 如何匹配包含子字符串的模式

c++ - 将 tag1 tag2 tag3 替换为 x1 x2 x3

powershell - 获取路径上图像文件的创建日期

c - 使用 char 从 MAC 地址输出 PIN

asp.net - 获取ASP.NET网站用户的MAC