Regex: Select everything from the beginning of the file to another string / word / tag
-
hello. I need to Select everything from the beginning of the file to
<body>
tag. I have to make a replacement in the PowerShell. this is the regex, seems good in notepad++, but in PowerShell is not working. Can anybody give me another good regex for me?\A(.*)$[\s\S]+(<body>)
$sourceFiles = Get-ChildItem 'c:\Folder1' $destinationFolder = 'c:\Folder2' foreach ($file in $sourceFiles) { $sourceContent = Get-Content $file.FullName -Raw $contentToInsert = [regex]::match($sourceContent,"\A(.*)$[\s\S]+(<body>)").value $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw $destinationContent = $destinationContent -replace '\A(.*)$[\s\S]+(<body>)',$contentToInsert Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8 } #end foreach file
-
got it, my regex formula was a little wrong. I put an extra
$
:)It should be:
\A(.*)[\s\S]+(<body>)
or\A(.*)[\s\S]+(<body>)|\2
(so, without $)$sourceFiles = Get-ChildItem 'c:\Folder1' $destinationFolder = 'c:\Folder2' foreach ($file in $sourceFiles) { $sourceContent = Get-Content $file.FullName -Raw $contentToInsert = [regex]::match($sourceContent,"\A(.*)[\s\S]+(<body>)").value $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw $destinationContent = $destinationContent -replace '\A(.*)[\s\S]+(<body>)',$contentToInsert Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8 } #end foreach file
-
Assuming the tag should not be matched, try this regex in Notepad++ v7.9.1:
Search: (?s)\A(.*?)(?=<body>)
Take care and have fun!
-
@Vasile-Caraus said in Regex: Select everything from the beginning of the file to another string / word / tag:
seems good in notepad++
As a reminder, this Forum is about Notepad++ . If a regex works in Notepad++, then this isn’t really the right place to ask about your problem.
I am glad the you figured it out, but let’s keep any regex discussion here on topic.