1

I am new in powershell and I have created the following script which it extracts what's between http:// and the next /, transform it and then replace the intial match:

$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 

I'm trying to make it better, maybe using regex or something else but not using hard coded text. Could anyone please help me with this ?

I was thinking something like:

$path = "myFile";
Get-Content $path | Foreach {$_ -replace "(?<=http://).+?(?=/.*)",".+?(?=/.*).newEnvironment"};
Set-Content $path;  

But It did not work, even if it was setting the links in this way:

http://.+?(?=/.*).newEnvironment/asd/test.aspx

1 Answer 1

2

It seems you want to

  • remove the "www." part
  • append the value of $newEnvironment to any URL

One way to do it would be to search for text that...

  • is preceded by "http://" — (?<=http://)
  • starts with "www." — www\.
  • has characters other than space or slash, as group 1 — ([^/ ]+)
  • for safety, is not already followed by $newEnvironment — (?!\.$newEnvironment)

and replace it with "regex group 1" + "." + $newEnvironment:

$fileName = "myfile"
$newEnvironment = "NewEnvironment"

$pattern = "(?<=http://)www\.([^/ ]+)(?!\.$newEnvironment)"
$replacement = "`$1.$newEnvironment"

(Get-Content $path) -replace $pattern,$replacement | Set-Content $path

Powershell operators are generally happy with arrays. Get-Content will give you an array of lines and -replace will work on all of them. (Another practical property of -replace is that you can chain it: "abc" -replace "a","A" -replace "b","B" will work.)

This means there is no need to write a manual foreach loop. The only thing required is a pair of parentheses so Get-Content does not mistake -replace for a parameter.

$1 is the back-reference to group 1, the backtick is PowerShell's escape character, because $ itself has meaning in both Powershell and regex.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot for the explanation! it worked fine after testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.