假設我有一個散列表:哈希表鍵語法來引用嵌入哈希表元素
$tokens = @{
Id=9999;
Title="Lorem ipsum dolor sit amet";
[email protected]{Name="John Doe"; Email='[email protected]'};
[email protected]{Name="Jane Doe"; Email='[email protected]'}
}
而且,我想填充的模板,與所述對應的散列表的值替換令牌(例如__Title__
):
/*
Author: __Author.Name__ <__Author.Email__>
Analyst: __Analyst.Name__ <__Analyst.Email__>
Request: __Title__ [__Id__]
*/
...
應該改爲:
/*
Author: John Doe <[email protected]>
Analyst: Jane Doe <[email protected]>
Request: Lorem ipsum dolor sit amet [9999]
*/
有沒有辦法來指代ŧ o在「父」哈希表中嵌入哈希表的元素?例如,$tokens['Author.Email']
不起作用。
代碼:
...
return [regex]::Replace($template, '__(?<tokenName>\w+)__', {
# __TOKEN__
param($match)
$tokenName = $match.Groups['tokenName'].Value
if ($tokens[$tokenName]) {
# matching token returns value from hashtable;
works for simple keys `$tokens['Title']`, not complex keys `$tokens['Author.Name']`
return $tokens[$tokenName]
}
else {
# non-matching token returns token
return $match
}
})
'$ tokens.author.email'是電子郵件地址。至少在PowerShell 3.0中 – Matt 2015-03-13 19:00:04
@Matt說了什麼。另外'$ tokens ['作者'] ['電子郵件']' – briantist 2015-03-13 19:03:30
@craig你是什麼意思? – Matt 2015-03-13 19:08:18