【发布时间】:2020-08-28 20:22:48
【问题描述】:
我正在尝试找到一个 powershell 命令来搜索目录中的所有文件,并仅在相对链接中用连字符替换任何下划线(链接不能以 http 开头)。
这是一个例子:
<a href="/always_sunny/is_the_best/">
应该变成
<a href="/always-sunny/is-the-best/">
但是,我希望正则表达式忽略以 http 开头的 href 值。所以应该忽略这样的链接。
<a href="http://thundergunexpress/always_sunny/"
以下是我一直在使用的当前 Powershell 命令和正则表达式。此正则表达式似乎在 Notepad ++ 中部分工作以查找和替换下划线,但不排除绝对链接。但是,正则表达式在 powershell 中根本不起作用,但我不确定这是由于正则表达式还是我对 Powershell 的了解有限。任何有关 Powershell 命令和正则表达式的帮助将不胜感激。
Get-ChildItem -Path k:\toolbox\powershell\ -recurse | ForEach {If (Get-Content $_.FullName | Select-String -Pattern '(\bhref="|(?!^)\G)[^"<_]*\K_'){(Get-Content $_ | ForEach {$_ -replace '(\bhref="|(?!^)\G)[^"<_]*\K_', '-'}) | Set-Content $_}}
【问题讨论】:
标签: regex powershell