PowerShell - Select tags between comments

Suzana Eree 811 Reputation points
2021-02-17T09:17:53.847+00:00

A simple example. I have this lines on 2 different html files, on Folder1 and Folder2. I want to parse (copy) some line from one folder to another, with REGEX, from the tag <tr> to </tr> tag.

The problem is that are a lots of <tr> and <tr> in the file, so I framed between the comments ( <!--START--> and <!--FINNISH--> ) the exact <tr> tags that I want to parse.

<html>
<body>
<table>

<!--START-->
<tr>
new contents
</tr>
<!--FINNISH-->

other lines 

<tr>
I love powershell because is hard to understand <tags/>
</tr>

other code

<tr>
keep stuff including <embedded/> <tags/>
</tr>

</table>
</body>
</html>

THE PROBLEM: How can I modify this PoweShell Code, as to select (with regex) only those <tr>and </tr> tags that are framed between the comments ( <!--START--> and <!--FINNISH--> )

$sourceFiles = Get-ChildItem 'c:\Folder1'
$destinationFolder = 'c:\Folder2'

foreach ($file in $sourceFiles) {

$sourceContent = Get-Content $file.FullName -Raw
$contentToInsert = [regex]::match($sourceContent,"(?ms)<tr>(.+)</tr>").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '(?ms)<tr>(.+)</tr>)',$contentToInsert

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8

} #end foreach file
Windows for IoT
Windows for IoT
A family of Microsoft operating systems designed for use in Internet of Things (IoT) devices.
381 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,171 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-02-17T11:16:02.137+00:00

    THE SOLUTION. It was all about a matter of REGEX, I had select all <!--START--> <!--FINNISH--> tags that also contains <tr> </tr> tags, so as to parse only their content

    $sourceFiles = Get-ChildItem 'c:\Folder1'  
    $destinationFolder = 'c:\Folder2'
    
    foreach ($file in $sourceFiles) {
    
    $sourceContent = Get-Content $file.FullName -Raw
    $contentToInsert = [regex]::match($sourceContent,"<!--START-->(\s*)<tr>[\s\S]+</tr>(\s*)<!--FINNISH-->").value
    $destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
    $destinationContent = $destinationContent -replace '<!--START-->(\s*)<tr>[\s\S]+</tr>(\s*)<!--FINNISH-->',$contentToInsert
    
    Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8
    
    } #end foreach file
    

0 additional answers

Sort by: Most helpful