Regex 在Powershell Regeg-match之后从$matches哈希表提取数据

Regex 在Powershell Regeg-match之后从$matches哈希表提取数据,regex,powershell,hashtable,Regex,Powershell,Hashtable,我已经花了一个多小时试图让一个简单的正则表达式工作。我的模式正在运行,我正在返回$Matches,正如我所读到的,它应该是一个哈希表。那么我怎样才能得到我所捕获的呢 代码: cls $keyword = "this is a 12345 test" $pattern = "\d{5}" $keyword -match $pattern $returnZipcode = "ERROR" Write-Host "GetZipCodeFromKeyword

我已经花了一个多小时试图让一个简单的正则表达式工作。我的模式正在运行,我正在返回$Matches,正如我所读到的,它应该是一个哈希表。那么我怎样才能得到我所捕获的呢

代码:

   cls 
   $keyword = "this is a 12345 test" 
   $pattern = "\d{5}"
   $keyword -match $pattern 
   $returnZipcode = "ERROR" 
   Write-Host "GetZipCodeFromKeyword RegEx `$Matches.Count=$($Matches.Count)" 
   $Matches | Out-String | Write-Host
   Write-Host "`$Matches[0].Value=$($Matches[0].Value)"
   Write-Host "`$Matches.Get_Item('0')=$($Matches.Get_Item("0"))"
   if ($Matches.Count -gt 0) 
      {
         $returnZipcode = $Matches[0].Value
      }

   # this is how hash tables work - why doesn't same work with the $Matches variable? 
   $states = @{"Washington" = "Olympia"; "Oregon" = "Salem"; California = "Sacramento"}
   $states | Out-String | Write-Host
   Write-Host "`$states.Get_Item('Oregon')=$($states.Get_Item("Oregon"))"
运行时结果:

Name                           Value  
----                           -----  
0                              12345  

$Matches[0].Value=
$Matches.Get_Item('0')=

Name                           Value
----                           -----
Washington                     Olympia
Oregon                         Salem  
California                     Sacramento 

$states.Get_Item('Oregon')=Salem

$Matches
只是一个哈希表,
Name
Value
列不是元素的属性
Name
只是键,
Value
是值

PS C:\> $Matches

Name                           Value
----                           -----
0                              12345


PS C:\> $Matches[0]
12345
如果希望使用
Get_Item
,但
$Matches
中的键是整数,而不是字符串:

PS C:\> $states.Get_Item('Oregon')
Salem
PS C:\> $Matches.Get_Item(0)
12345

与其他一些语言不同,并非所有哈希表键都必须是字符串,除非您告诉它,否则Powershell通常不会将数字转换为字符串或从字符串转换为字符串。

谢谢,刚刚在这里发布了后续消息:我以为真/假是哈希表的一部分,但仍不确定原因。