背景:我将 .mp4 视频的文件名更改为小写,并替换了特殊字符和空格。现在我必须以类似的方式更改 .txt 文件中的关联 URL。有许多文本文件包含大量引用视频的这些 URL。
问题:我应该替换任何文本文件中“flashplayer”和“/flashplayer”之间的每个字符串中的特殊字符,但不得更改flashplayer 标签之外的任何内容。
我不知道如何选择“flashplayer”和“/flashplayer”之间的字符串进行替换。
示例字符串:
(flashplayer width="640" height="480" position="1")file=/wiki/data/media/sales/a/ö 2.mp4&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0(/flashplayer)
此示例包含在文本文件(DokuWiki 页面)中。() 暗示标记字符。
示例输出字符串:
(flashplayer width="640" height="480" position="1")file=/wiki/data/media/sales/a/oe_2.mp4&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0(/flashplayer)
用 rename-item 替换应该是:
- ä = ae
- ö = oe
- ü = ü
- ' ' = '_'
更新:脚本看起来像:
# vars (User-Eingabe)
$source = "d:\here\name\test\pages"
$search = '(\<flashplayer.*?\>file\=/wiki/87sj38d/media)(.*?)(\<\/flashplayer\>)'
$a = 1
Write-Host "`nSource:`t $source`n"
# replace special characters
gci $source -r -Filter *.txt | ForEach-Object {
$text = Get-Content $_.FullName | ForEach-Object {
if($_ -match $search) {
$_ -replace [Regex]::Escape($Matches[2]), ($Matches[2] -replace'ö', 'oe' -replace'ä', 'ae' -replace'ü', 'ue' -replace'\s', '_' )
$output = $Matches[2]
$tags = $a++
Write-Host "`nTag $tags : $output"
} else {
$_
}
}
$text | Set-Content $_.FullName
}
文本文件包含一行代码,如下所示:
{{backlinks>path:product:description:kennwort_aendern}}
该脚本仅在我删除这行代码时才有效。否则 flashplayertags 之间的字符串保持不变。令人困惑的是,替换有时会起作用,有时不会。flashplayertags 之间的字符串可以包含许多特殊字符。请参阅示例字符串:
<flashplayer_width="640"_height="480"_position="1">file=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.mp4&image=/wiki/87sj38d/media/ab/any/test/1001_Grundlagen Kennwort ändern.jpg&config=/wiki/lib/plugins/flashplayer/config_video.xml&start=0</flashplayer>
Write-Host $output 正确显示所有字符串,但替换无法正常运行。