Community
    • Login

    Regex: Select everything from the beginning of the file to another string / word / tag

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 3 Posters 396 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Vasile CarausV
      Vasile Caraus
      last edited by Vasile Caraus

      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
      
      astrosofistaA PeterJonesP 2 Replies Last reply Reply Quote 0
      • Vasile CarausV
        Vasile Caraus
        last edited by Vasile Caraus

        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
        
        1 Reply Last reply Reply Quote 0
        • astrosofistaA
          astrosofista @Vasile Caraus
          last edited by

          Hi @Vasile-Caraus

          Assuming the tag should not be matched, try this regex in Notepad++ v7.9.1:

          Search: (?s)\A(.*?)(?=<body>)
          

          Take care and have fun!

          1 Reply Last reply Reply Quote 1
          • PeterJonesP
            PeterJones @Vasile Caraus
            last edited by

            @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.

            1 Reply Last reply Reply Quote 1
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors