How to run PowerShell, as a Batch Process file txt/html with REGEX (search and replace)?

Suzana Eree 811 Reputation points
2021-03-10T16:24:19.33+00:00

hello. I have more regex to run on multiple html files from Folder1. I must run more REGEX with search and replace, for example:

SEARCH: (?-s)(".+?") REPLACE BY: $0
SEARCH: (^.*?)=(.*$) Replace by: \1\r\n\2
SEARCH: ^.(.*)$ REPLACE BY: \1

I mage a PowerShellp script, I add those 3 regex search and replace formulas, but is not working. Can anyone help me?

$sourceFiles = Get-ChildItem 'c:\Folder1'  
$destinationFolder = 'c:\Folder1'
foreach ($file in $sourceFiles) {
$sourceContent = Get-Content $file.FullName -Raw

$contentToInsert = [regex]::match($sourceContent,"(?-s)(".+?")").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '$0',$contentToInsert

$contentToInsert = [regex]::match($sourceContent,"(^.*?)=(.*$)").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '\1\r\n\2',$contentToInsert

$contentToInsert = [regex]::match($sourceContent,"^.(.*)$").value
$destinationContent = Get-Content $destinationFolder\$($file.Name) -Raw
$destinationContent = $destinationContent -replace '\1',$contentToInsert

Set-Content -Path $destinationFolder\$($file.Name) -Value $destinationContent -Encoding UTF8
} #end foreach file
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,560 questions
0 comments No comments
{count} votes

Accepted answer
  1. Suzana Eree 811 Reputation points
    2021-03-13T21:42:55.037+00:00

    got it. This is the solution:

    $path = 'c:\Folder1\file1.html'
    $result = 'c:\Folder1\result.html'
    Get-Content -Path $path | ForEach-Object{ 
        $one = $_ -replace '(?<=<li>)\s+','CARPET' #replace First Regex with the word CARPET
        $two = $one -replace 'CARPET','DOOR' #replace the word CARPET with DOOR
        ($three = $two -replace 'DOOR','BEAUTIFUL') | Out-File -FilePath $result -Append #replace the word DOOR with BEAUTIFUL
        "Final = $three"
    }
    
    0 comments No comments

10 additional answers

Sort by: Most helpful
  1. Ian Xue 37,711 Reputation points Microsoft Vendor
    2021-03-12T06:44:03.393+00:00

    Hi @Suzana Eree ,

    The two regexes don't work as neither '\K' nor '\h' is valid in powershell. To match the space you can use '\s' or the space character ' '.

    $path = 'D:\temp\file.html'  
    $result = 'D:\temp\result.html'  
    Get-Content -Path $path | ForEach-Object{   
        ' ' + ($_ -replace '(?<=<li>)\s+','') + ' ' | Out-File -FilePath $result -Append       
        }  
    

    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.

    0 comments No comments

  2. Suzana Eree 811 Reputation points
    2021-03-12T07:37:30.687+00:00

    thanks, @Ian Xue works, but as to understand much better the Batch process (multiple actions, one by one in order), I will give another example, with 4 simple regex that must run in order I give:

    SEARCH: (?<=<li>)\s+ REPLACE BY: BEBE
    SEARCH: BEBE REPLACE BY: OANA
    SEARCH: OANA REPLACE BY: \s
    SEARCH: <li>\s REPLACE BY: '' (by nothing, as to remove the space)

    $path = 'c:\Folder1\file1.html'  
     $result = 'c:\Folder1\result.html'  
     Get-Content -Path $path | ForEach-Object{   
        ($_ -replace '(?<=<li>)\s+','BEBE') | Out-File -FilePath $result -Append    
        ($_ -replace 'BEBE','OANA')  | Out-File -FilePath $result -Append     
        ($_ -replace 'OANA','\s')  | Out-File -FilePath $result -Append   
        ($_ -replace '<li>\s','')  | Out-File -FilePath $result -Append   
             }  
    

    It is not working. What did i do wrong?

    0 comments No comments

  3. Rich Matheisen 46,896 Reputation points
    2021-03-13T02:56:10.18+00:00

    The output of each replace operation is the string resulting from the replace. If we're to assume that the 1st regex put BEBE into the string you'd see that in output file. But for the 2nd, 3rd, and 4th regex you're operating on the original string (i.e. the on from your input file), not the resulting string from the 1sr regex. Because of that you won't find BEBE or OANA -- so the string from the file is sent to your output file. The 4th regex should work (if the 1st one did) so it should have erased the "<li> " and written the result to the output file.

    If you want to see the progression of changes you'd have to operate on the result of the previous replace operation, not the original string.

    Like this:

    $one = "XXXX"
    ($two = $one -replace "XX$","xx") | Out-String
    ($three = $two -replace "^XX","aa") | Out-String
    ($four = $three -replace "ax","BB") | Out-String
    "Final = $four"
    
    0 comments No comments

  4. Suzana Eree 811 Reputation points
    2021-03-13T07:51:56.717+00:00

    thanks, @Rich Matheisen can you please write the entire code, also with $Path, $Result, Get content..etc, so as to ACCEPT it as a complete answer ?

    also, if you can give some comments for other people that will view this post. For example *** XX will replace AA *** , *** ax will be replace by BB ***

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.