Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 从powershell中的文本文件替换http链接中的内容_Regex_Powershell - Fatal编程技术网

Regex 从powershell中的文本文件替换http链接中的内容

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

我是powershell的新手,我创建了以下脚本,它提取http://和next/之间的内容,对其进行转换,然后替换初始匹配:

$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.”
    部件
  • $newEnvironment
    的值附加到任何URL
一种方法是搜索


  • 前面是“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