我的正則運算式適用于提取字串內容的各個區域,但是當我將其放入代碼中以獲取鍵值對的映射時,映射是錯誤的。有任何想法嗎?
重復內容如下所示,alarmID 右側不同,如 12-5000,鍵名如 LOAD_MEDIA:
<?xml version="1.0" encoding="utf-16"?>
<Configuration>
<Key Name="Alarms">
<Key Name="LOAD_MEDIA">
<Value Name="AlarmID">12-5000</Value>
<Value Name="blah">Dialog.Cut</Value>
<Value Name="blah1"></Value>
<Value Name="blah2">Dialog.Cancel</Value>
<Value Name="blah3">false</Value>
<Value Name="blah4">true</Value>
<Key Name="Sizes">
<Key Name="57x">
<Value Name="blah1">7</Value>
<Value Name="blah2">9</Value>
<Value Name="blah3">Dialog.57</Value>
</Key>
<Key Name="88">
<Value Name="blah1">10</Value>
<Value Name="blah2">10</Value>
<Value Name="blah3">Dialog.88</Value>
</Key>
<Key Name="810">
<Value Name="blah1">10</Value>
<Value Name="blah2">12</Value>
<Value Name="blah3">Dialog.810</Value>
</Key>
<Key Name="114">
<Value Name="blah1">1</Value>
<Value Name="blah2">1</Value>
<Value Name="blah3">Dialog.114</Value>
</Key>
<Key Name="10">
<Value Name="blah1">1</Value>
<Value Name="blah2">2</Value>
<Value Name="blah3">Dialog.10</Value>
</Key>
</Key>
</Key>
<Key Name="LOAD_MEDIA2">
<Value Name="AlarmID">12-5001</Value>
<Value Name="blah">Dialog.Cut</Value>
<Value Name="blah1"></Value>
<Value Name="blah2">Dialog.Cancel</Value>
<Value Name="blah3">false</Value>
<Value Name="blah4">true</Value>
<Key Name="Sizes">
<Key Name="57x">
<Value Name="blah1">7</Value>
<Value Name="blah2">9</Value>
<Value Name="blah3">Dialog.57</Value>
</Key>
<Key Name="88">
<Value Name="blah1">10</Value>
<Value Name="blah2">10</Value>
<Value Name="blah3">Dialog.88</Value>
</Key>
<Key Name="810">
<Value Name="blah1">10</Value>
<Value Name="blah2">12</Value>
<Value Name="blah3">Dialog.810</Value>
</Key>
<Key Name="114">
<Value Name="blah1">1</Value>
<Value Name="blah2">1</Value>
<Value Name="blah3">Dialog.114</Value>
</Key>
<Key Name="10">
<Value Name="blah1">1</Value>
<Value Name="blah2">2</Value>
<Value Name="blah3">Dialog.10</Value>
</Key>
</Key>
</Key>
</Key>
</Configuration>
..重復格式
出于某種原因,我回傳的 errorMap 甚至沒有接近正確。
這是應該在 $errorMap 中回傳鍵值對的代碼:
function Get-AlarmIDs{
[cmdletbinding()]
Param ([string]$fileContent)
# create an ordered hashtable to store the results
$errorMap = [ordered]@{}
# process the lines one-by-one
switch -Regex ($fileContent -split '\r?\n') {
'[\t] ?<Key Name="(.*)">[\r?\n\s] ?<Value' { #match Key Name followed by Value
if(-not($matches[1] -match '[0-9]')) #use if it's not something like 57
{
$key = ($matches[1]).Trim()
}
}
'AlarmID">(.*)<' { #match AlarmID like 12-5700
$errorMap[$key] = ($matches[1]).Trim()
}
}
return $errorMap
}
我希望地圖看起來像這樣:
LOAD_MEDIA, 12-5000
LOAD_MEDIA2, 12-3
...
我知道我在 regex101.com 上的測驗表明我的鍵和值分別獲取了所需的行,但是當我將它們放在代碼中時,地圖全亂了:
[有序詞典:1]
[0]: [6,"12-5900"]
我不確定它從哪里得到 6,而 12-5900 大約是重復序列中的最后一個,應該在 12-5900 之前回傳 5。
I'm using powershell 5.1 and Visual Studio code.
Update:
I'm trying to do this the xml way, and not sure how to get out of the xml what I need.
[xml]$xmlElm = (select-xml -xpath / -path $fileNamePath).node #Get-Content $fileNamePath
$xmlElem.Configuration.'Key Name'.'Key Name'.Value #this doesn't print anything...shouldn't it?
Update2:
Trying some more...
[xml]$xmldocument = Get-Content $fileNamePath
$tmp1 = $xmldocument.ChildNodes.selectNodes("*")
$tmp2 = $xmldocument.ChildNodes.selectNodes("*/Key")
$tmp3 = $xmldocument.Key.Value.AlarmID[0] #this is indexing into null
$tmp4 = $xmldocument.Key.Value #this is null
Update3:
I'm trying what's in Santiago's answer, but get blank Value in the map.
[xml]$xmldocument = Get-Content $fileNamePath
$result = foreach($key in $xmldocument.Configuration.Key.Key)
{
[pscustomobject]@{
Key = $key.Name
Value = $key.Value.Where{$_Name -eq 'AlarmID'}.'#text'
}
}
Gives (blank values)
[0]: @{Key=LOAD_MEDIA; Value=}
[1]: @{Key=Load_MEDIA2; Value=}
I'm not sure what's different.
Update4:
$result = foreach($key in $xmldocument.Configuration.Key.Key)
{
[pscustomobject]@{
Key = $key.Name
Value = $key.Value
}
}
return $result
When I look at $result, it looks like this:
[0]: @{Key=LOAD_MEDIA; Value=System.Object[]}
[1]: @{Key=Load_MEDIA2; Value=System.Object[]}
uj5u.com熱心網友回復:
可能有比這更好的方法,但就目前而言,這應該可以幫助您獲得所需的內容。假設您已經在變數中加載了 XML,在這種情況下$xml:
$result = foreach($key in $xml.Configuration.Key.Key) {
[pscustomobject]@{
Key = $key.Name
Value = $key.Value.Where{$_.Name -eq 'AlarmID'}.'#text'
}
}
$result使用有問題的 XML 應該如下所示:
Key Value
--- -----
LOAD_MEDIA 12-5000
LOAD_MEDIA2 12-5001
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/427085.html
標籤:regex powershell
上一篇:正則運算式從多到少找到重復的句子
下一篇:正則運算式:與特定長度的數字相反
