use -Raw
This is a solution !
$path = 'c:\Folder1\file1.html'
(Get-Content -Path $path -Raw) -replace '(<title>).*?(</title>)', '$1NEW is now there!$2' |
Set-Content -Path $Path
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
hi can someone tell me what about this PowerShell code?
Someone made this code for me. Basically you use a regular expression to change a few lines in an html file. I think it must be filled with something for it to work. Can anyone correct it, as to make it work fine?
So, basically, I have an html file with some tags such as <title>
and I want to select everything between tags and make a replace with a regex formula. but something wrong.
$Content = Get-Content -Path $Path "c:\Users\Castel\Videos\Captures" -Filter "*.html"
Set-Content -Path $Path -Value $Content
# Get each page as a HTML file/ assign to a variable
#$Htmltext = (Invoke-WebRequest -Uri $MainUrl)# This is the link to the webpage
#$HtmlPage.content
foreach($Line in $HtmlPage)
{
$GetTitle = [regex] "$RegexForTitle"
$PageTitle = $FindTitle.Match($HtmlPage)
$PageTitle.Captures[0].Value
$Title = $PageTitle.Captures[0].Value -ireplace '<[^\>]*>,'$1' #This regex selects everything between tags and make a replace:
} #end foreach file
use -Raw
This is a solution !
$path = 'c:\Folder1\file1.html'
(Get-Content -Path $path -Raw) -replace '(<title>).*?(</title>)', '$1NEW is now there!$2' |
Set-Content -Path $Path
Hi @Suzana Eree ,
Do you want to replace titles between <title> and </title> with the two characters '$1'? If so, you can simply do it like this
$path = 'D:\temp\file.html'
$Content = Get-Content -Path $path
$newContent =@()
$RegexForTitle = '(?<=title>).*(?=</title>)'
foreach($Line in $Content)
{
$newContent += $Line -replace $RegexForTitle,'$1'
}
Set-Content -Path $Path -Value $newContent
Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
yes, works.
but, in other case, the replace of $1
does not working.
suppose I have the following Search and Replace with Regex:
FIND: (?<=title>).*(?=</title>)
REPLACE BY $1 OTHER TITLE
So, I want to change the title, the words between <title> .. </title>
tags. Your $1
does not working, because it must not be seen, only OTHER TITLE
Basically, it is a REGEX FORMULA
replaced with OTHER REGEX FORMULA
, NOT Regex Formula
replace with A Word
.
In your last example there's no matching group. $1 would represent the 1st matching group. Add parentheses around the ".*".
"<title>Something goes here</title>" -replace '(?<=title>)(.*?)</title>', '$1 NEW is now there!'
<title>Something goes here NEW is now there!
There's really no place to put the code in your original example because the example needs to be fixed so it at least makes sense.
However, taking your example as a small framework, this:
$Content = "<bogus></bogus>", "<title>Something goes here</title>","<TheEnd>END</TheEnd>"
foreach($Line in $Content){
$Line -replace "<title>(.*?)</title>",'$1 NEW is now there!' #This regex selects everything between tags and make a replace:
}
Or this:
$Content = "<bogus></bogus>", "<title>Something goes here</title>","<TheEnd>END</TheEnd>"
$GetTitle = [regex]"<title>(.*?)</title>"
foreach($Line in $Content){
$Line -replace $GetTitle,'$1 NEW is now there!' #This regex selects everything between tags and make a replace:
}
Or this:
$Content = "<bogus></bogus>", "<title>Something goes here</title>","<TheEnd>END</TheEnd>"
$GetTitle = [regex]"<title>(.*?)</title>"
foreach($Line in $Content){
$GetTitle.Replace($Line, '$1 New is now there!')
}
Each produces:
<bogus></bogus>
Something goes here NEW is now there!
<TheEnd>END</TheEnd>