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 696 Views 2 Watching
    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 Offline
      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 Offline
        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 Offline
          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 Offline
            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

            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
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors