這個問題更多是關于我對Powershell的物件的理解,而不是解決這個實際的例子。我知道還有其他方法可以從字串中分離出頁碼。
在我的示例中,我想通過訪問管道模式匹配的物件匹配值來做到這一點。
# data
$headerString = 'BARTLETT-BEDGGOOD__PAGE_5 BEECH-BEST__PAGE_6'
# require the number of page only
$regexPageNum = '([0-9]$)'
# split the header string into two separate strings to access page numbers
[string[]]$pages = $null
$pages = $headerString -split ' '
# access page numbers using regex pattern
$pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object {$_.Matches.Value}
輸出是:
$_.Matches.Value
----------------
5
好的。到目前為止,一切都很好。我看到了陣列成員的頁碼pages[0]但是我如何從物件中獲取這個值?以下不起作用。
$x = $pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object {$_.Matches.Value}
Write-Host "Here it is:"$x
輸出:
Here it is: @{$_.Matches.Value=5}
不是將值分配給Powershell 分配5的變數$x,而是在我看來:一個以物件描述作為其唯一成員的哈希表?
但是,如果我嘗試使用“Brackets for Access”參考訪問我的變數:hashtables Powershell 表明變數 $x 實際上是一個陣列。
x = $pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object {$_.Matches.Value}
Write-Host "Here it is:"$x
$y = $x[$_.Matches.Value]
Write-Host "What about now:"$y
輸出:
Here it is: @{$_.Matches.Value=5}
InvalidOperation:
Line |
33 | $y = $x[$_.Matches.Value]
| ~~~~~~~~~~~~~~~~~~~~~~~~~
| Index operation failed; the array index evaluated to null.
What about now:
好的。在這個階段,我知道我很傻。但我要說明的一點是:當我使用完 Powershell 物件后,如何檢索我想要的值?
uj5u.com熱心網友回復:
您可以使用$x.{ $_.Matches.Value }來訪問該值。
$x = $pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object { $_.Matches.Value }
$x.{ $_.Matches.Value } # This will print 5
即,您必須將屬性名稱包裝在里面,{}因為屬性名稱包含“。”
而不是這種方式,我建議您創建一個計算屬性,使用Select-Object它使代碼更具可讀性。
$x = $pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object @{Name = 'PageNumber'; Expression = {$_.Matches.Value}}
$x.PageNumber
uj5u.com熱心網友回復:
#Access matches in case of single match
$x = "red blue yellow green" | select-string -Pattern 'blue'
$x.matches.value
#Output
blue
#Access matches in case of multi match
$x = "red blue yellow green blue" | select-string -Pattern 'blue' -AllMatches
$x.matches.value
#Output
blue
blue
uj5u.com熱心網友回復:
當您使用腳本塊作為Select-Object回傳值的引數時,將包含一個名稱與腳本塊的源代碼匹配的屬性...
PS> @{ "aaa" = "bbb" } | select-object { $_.aaa; <# xxx #> }
$_.aaa; <# xxx #>
-------------------
bbb
在這種病態的情況下,如果我想訪問屬性,我不能使用默認的“點”表示法中的名稱,因為它包含保留字符,但是如果參考屬性名稱,則可以訪問它:
PS> $x = @{ "aaa" = "bbb" } | select-object { $_.aaa; <# xxx #> }
# note the leading and trailing spaces in the string because the
# the original scriptblock source contains spaces between the "{" and "}"
PS> $x.' $_.aaa; <# xxx #> '
bbb
在你的情況下,你會這樣做:
PS> $x = $pages[0] | Select-String -AllMatches -Pattern $regexPageNum | Select-Object {$_.Matches.Value}
PS C:\Users\Mike> $x.'$_.Matches.Value'
其他選項也可以:
$x = $pages[0] `
| Select-String -AllMatches -Pattern $regexPageNum `
| Select-Object {$_.Matches.Value}
# get the property whose name is contained in the $name variable
PS> $name = '$_.Matches.Value'
PS> $x.$name
5
# the scriptblock gets converted into a string, and then that string
# is used as a property name
PS> $x.{$_.Matches.Value}
5
# note the whitespace in both scriptblocks has to match *exactly* otherwise the property name won't be found
PS> $x.{ $_.Matches.Value }
ParentContainsErrorRecordException: The property ' $_.Matches.Value ' cannot be found on this object. Verify that the property exists.
但...
有一種更簡單的方法 - 如果您將哈希表而Select-Object不是腳本塊傳遞給您可以指定屬性的名稱 - 例如
PS> $x = $pages[0] `
| Select-String -AllMatches -Pattern $regexPageNum `
| Select-Object @{ "l"="Count"; "e"={$_.Matches.Value} }
PS> $x
Count
-----
5
PS> $x.Count
5
參考:
- about_Calculated_Properties - 哈希表鍵定義
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/522380.html
標籤:电源外壳目的匹配分配
上一篇:在python中編輯Json
