• Login
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 402 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.
  • V
    Vasile Caraus
    last edited by Vasile Caraus Feb 14, 2021, 12:43 PM Feb 14, 2021, 12:42 PM

    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
    
    A P 2 Replies Last reply Feb 14, 2021, 1:53 PM Reply Quote 0
    • V
      Vasile Caraus
      last edited by Vasile Caraus Feb 14, 2021, 12:53 PM Feb 14, 2021, 12:53 PM

      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
      • A
        astrosofista @Vasile Caraus
        last edited by Feb 14, 2021, 1:53 PM

        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
        • P
          PeterJones @Vasile Caraus
          last edited by Feb 15, 2021, 2:46 AM

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