为什么我得到的提取物“真”或“假”(当所有我想回去只是邮政编码)这个函数的结果:
Function GetZipCodeFromKeyword([String] $keyword)
{
$pattern = "\d{5}"
$keyword -match $pattern
$returnZipcode = "ERROR"
#Write-Host "GetZipCodeFromKeyword RegEx `$Matches.Count=$($Matches.Count)"
if ($Matches.Count -gt 0)
{
$returnZipcode = $Matches[0]
}
Write-Host "`$returnZipcode=$returnZipcode"
return $returnZipcode
}
cls
$testKeyword = "Somewhere in 77562 Texas "
$zipcode = GetZipCodeFromKeyword $testKeyword
Write-Host "Zip='$zipcode' from keyword=$testKeyword"
Write-Host " "
$testKeyword = "Somewhere in Dallas Texas "
$zipcode = GetZipCodeFromKeyword $testKeyword
Write-Host "Zip='$zipcode' from keyword=$testKeyword"
运行时间结果:
$returnZipcode=77562
Zip='True 77562' from keyword=Somewhere in 77562 Texas
$returnZipcode=12345
Zip='False 12345' from keyword=Somewhere in Dallas Texas