Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex PowerShell正则表达式到拆分MAC地址_Regex_Powershell_Mac Address_Capture Group - Fatal编程技术网

Regex PowerShell正则表达式到拆分MAC地址

Regex PowerShell正则表达式到拆分MAC地址,regex,powershell,mac-address,capture-group,Regex,Powershell,Mac Address,Capture Group,我需要使用正则表达式验证原始格式的MAC地址,并将其拆分为一个由6个值和2个字符组成的数组 当我使用以下模式时,我只获取捕获组最后一次迭代的内容: PS C:\Windows\System32> "708BCDBC8A0D" -match "^([0-9a-z]{2}){6}$" True PS C:\Windows\System32> $Matches Name Value ----

我需要使用正则表达式验证原始格式的MAC地址,并将其拆分为一个由6个值和2个字符组成的数组
当我使用以下模式时,我只获取捕获组最后一次迭代的内容:

PS C:\Windows\System32> "708BCDBC8A0D" -match "^([0-9a-z]{2}){6}$"
True
PS C:\Windows\System32> $Matches

Name                           Value
----                           -----
1                              0D
0                              708BCDBC8A0D


PS C:\Windows\System32>
我可以用什么样的模式来控制所有的小组
我需要这个结果:

0 = 708BCDBC8A0D
1 = 70
2 = 8B
3 = CD
4 = BC
5 = 8A
6 = 0D

不能使用单个组定义捕获多个组。 避免在不必要时使用正则表达式,因为它需要大量CPU。对数以百万计的读者来说很有价值

对于Mac,您可以使用特殊的
PhysicalAddress
class:

[System.Net.NetworkInformation.PhysicalAddress]::Parse('708BCDBC8A0D') 
对于.Net 5(Powershell Core我认为基于它的),添加了
TryParse
方法,但在.Net 4.5中没有
TryParse
方法

要检查.Net framework powershell是否正在运行,请使用
[System.Reflection.Assembly]::GetExecutionGassembly().ImageRuntimeVersion




注:我相信这个答案是正确的,希望是有帮助的;如果你不同意,告诉我们原因

正如您所观察到的,反映最新(标量输入[1])结果的只包含嵌入式捕获组(
(…)
)捕获的内容的最后一个实例

通常,
-match
在输入中最多只查找一个匹配项

  • 建议引入一个新的
    -matchall
    操作符,该操作符将查找所有匹配项并将它们作为数组返回
直接使用作为PowerShell正则表达式功能基础的
[regex]
类()已经提供了这种能力,在这种情况下甚至不需要捕获组

# Note: Inline option (?i) makes the regex case-INsensitive
#       (which PowerShell's operators are BY DEFAULT).
PS> [regex]::Matches('708BCDBC8A0D', '(?i)[0-9a-e]{2}').Value
70
8B
CD
BC
8A
0D
但是,通过一些技巧,您还可以使用
-split
,即:

注:

  • 正则表达式必须包含在捕获组
    (…)
    中,以明确指示
    -split
    在结果中包含它匹配的内容;由于正则表达式通常描述感兴趣的子字符串之间的分隔符,因此通常不包括其匹配项

  • 在这种情况下,我们只关心“分隔符”,而它们之间的子字符串在这里是空字符串,因此我们使用
    -ne'
    过滤掉它们



[1] 如果
-match
操作的LHS是一个数组(集合),则对每个元素进行匹配,并返回匹配元素的子数组(而不是单个布尔值)。在这种情况下,
$Matches
未填充。

我知道使用RegEx时CPU负载较高,但我还是需要它来验证MAC地址。使用
*6
的“Hack”最好不要重复定义。这个PowerShell方法
@(0..5)|用于每个对象{'708bc8a0d'。子字符串($2,2)}
。谢谢你的提示!也许使用
[System.Net.NetworkInformation.PhysicalAddress]::Parse('708BCDBC8A0D')
更适合您的情况?至于底层PowerShell版本的.Net版本:从当前的稳定版本PowerShell 7.1.3开始,它是.Net 5.0.4。v7.2可能基于.NET6.0(尚未发布)。您可以在PowerShell(Core)会话中运行以下命令以确定底层.NET版本:
(从Json转换(获取内容-Raw“$PSHOME/pwsh.runtimeconfig.Json”)).runtimeOptions.includedframes.version
这是否回答了您的问题<代码>[System.Net.NetworkInformation.PhysicalAddress]::Parse('708BCCD8A0D')。GetAddressBytes()|%ToString X2看起来像是@iRon的链接回答了这个问题。
'@(0..5) | ForEach-Object {'708BCDBC8A0D'.Substring($_ * 2, 2)}'
@(
    [String]::new('708BCDBC8A0D'[0..1]),
    [String]::new('708BCDBC8A0D'[2..3]),
    [String]::new('708BCDBC8A0D'[4..5]),
    [String]::new('708BCDBC8A0D'[6..7]),
    [String]::new('708BCDBC8A0D'[8..9]),
    [String]::new('708BCDBC8A0D'[10..11])
)
# Note: Inline option (?i) makes the regex case-INsensitive
#       (which PowerShell's operators are BY DEFAULT).
PS> [regex]::Matches('708BCDBC8A0D', '(?i)[0-9a-e]{2}').Value
70
8B
CD
BC
8A
0D
# Note: No inline option needed: -split - like -match and -replace -
#       is case-INsensitive by default.
PS> '708BCDBC8A0D' -split '([0-9a-e]{2})' -ne ''
70
8B
CD
BC
8A
0D