Regex 从powershell中的文本文件替换http链接中的内容
我是powershell的新手,我创建了以下脚本,它提取http://和next/之间的内容,对其进行转换,然后替换初始匹配:Regex 从powershell中的文本文件替换http链接中的内容,regex,powershell,Regex,Powershell,我是powershell的新手,我创建了以下脚本,它提取http://和next/之间的内容,对其进行转换,然后替换初始匹配: $fileName = "myfile" $newEnvironment = "NewEnvironment" $config = Get-Content $fileName $newConfig = $config | % { $_ -replace "http://www.site.de", "http://site.de.$newEnvironment" } $n
$fileName = "myfile"
$newEnvironment = "NewEnvironment"
$config = Get-Content $fileName
$newConfig = $config | % { $_ -replace "http://www.site.de", "http://site.de.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.com.tr", "http://site.com.tr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.fr", "http://site.fr.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.pl", "http://site.pl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.be", "http://site-1.be.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site-1.nl", "http://site-1.nl.$newEnvironment" }
$newConfig = $newConfig | % { $_ -replace "http://www.site.it", "http://site.it.$newEnvironment" }
$newConfig | Set-Content $fileName
我正在努力使它更好,也许使用正则表达式或其他东西,但不使用硬编码文本。
谁能帮我一下吗
我的想法是:
$path = "myFile";
Get-Content $path | Foreach {$_ -replace "(?<=http://).+?(?=/.*)",".+?(?=/.*).newEnvironment"};
Set-Content $path;
看来你想
- 卸下
部件“www.”
- 将
的值附加到任何URL$newEnvironment
- 前面是“http://”-
(?非常感谢您的解释!测试后效果很好。
http://.+?(?=/.*).newEnvironment/asd/test.aspx
$fileName = "myfile" $newEnvironment = "NewEnvironment" $pattern = "(?<=http://)www\.([^/ ]+)(?!\.$newEnvironment)" $replacement = "`$1.$newEnvironment" (Get-Content $path) -replace $pattern,$replacement | Set-Content $path