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.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login