function ReplaceHtmlCharacterCodes {
param(
[string] $pattern
)
# Import the System.Web assembly to use the WebUtility class
Add-Type -AssemblyName System.Web
# Decode the captured text
$decodedText = [System.Web.HttpUtility]::HtmlDecode($pattern)
return $decodedText
}
$record = $record -replace 'amp;', '&'
$decodedrecord = $record | ForEach-Object {
if ($_ -match $pattern) {
$_ -replace $pattern, {
ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern
}
} else {
$_
}
}
给我的输出如下: 孤独是最危险且最少被谈论的流行病
ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern
给予生命的友谊?在《为人而生》一书中,畅销书作家兼《共同规则》的创始人贾斯汀·惠特梅尔·厄利 (Justin Whitmel Earley) 解释了为什么我们是为友谊而生,以及我们如何在技术驱动的大流行后世界中培养友谊。
我想转义这些 html 字符,但我得到的是:
`ReplaceHtmlCharacterCodes ($pattern) # Pass the pattern`
语法
... -replace <pattern>,{ <substitution routine> }
直到PowerShell 6.1才引入。
要在 Windows PowerShell 中使用匹配评估器,请直接使用
[regex]::Replace()
:
[regex]::Replace($_, $pattern, [System.Text.RegularExpressions.MatchEvaluator]{return ReplaceHtmlCharacterCodes $pattern})