Loop search and replace two-part string over file using PowerShell while preserving one of the parts











up vote
2
down vote

favorite












I am new at PowerShell and have not found a Stack Overflow question or a documentation reference that gets me all the way to a successful outcome. If a question or documentation reference already exists that answers this that I overlooked I would be grateful to know.



In a text file is a string like this:



<span><span><span><span><span></span></span></span></span></span>


The number of <span> and </span> varies from file to file. For example, in some files it is like this:



<span></span>


Yet in others it is like this:



<span><span></span></span>


And so on. There are likely never going to be more than 24 of each in a string.



I want to eliminate all strings like this in the text file, yet preserve the </span> in strings like this:



<span style="font-weight:bold;">text</span>


There may be many variations on that kind of string in the text file; for example, <span style="font-size: 10px; font-weight: 400;">text</span> or <span style="font-size: 10px; font-weight: 400;">text</span> and I don't know beforehand what variation(s) will be included in the text file.



This partially works...



$original_file = 'in.txt'
$destination_file = 'out.txt'

(Get-Content $original_file) | Foreach-Object {
$_ -replace '<span>', '' `
-replace '</span>', ''
} | Set-Content $destination_file


...but obviously results in something like <span style="font-weight:bold;">text.



In the PowerShell script above I can use



    $_ -replace '<span></span>', '' `


But of course it only catches the <span></span> in the middle of the string because, as it is written now, it does not loop.



I know it is silly to do something like this



$original_file = 'in.txt'
$destination_file = 'out.txt'

(Get-Content $original_file) | Foreach-Object {
$_ -replace '<span></span>', '' `
-replace '<span></span>', '' `
-replace '<span></span>', '' `
-replace '<span></span>', '' `
-replace '<span></span>', ''
} | Set-Content $destination_file


So because the <span> string collapses into itself each time the script is run, producing a new inner <span></span> that can then be removed, the best solution I can think of is to loop the script over the file until it recognizes that all instances of <span></span> are gone.



I feel like adding logic along these lines is necessary:



   foreach($i in 1..24){
Write-Host $i


But have not been able to successfully incorporate it into the script.



If this is the wrong approach entirely I would be grateful to know.



The reason for PowerShell is that my team prefers it for scripts included in an Azure DevOps release pipeline.



Thanks for any ideas or help.










share|improve this question




























    up vote
    2
    down vote

    favorite












    I am new at PowerShell and have not found a Stack Overflow question or a documentation reference that gets me all the way to a successful outcome. If a question or documentation reference already exists that answers this that I overlooked I would be grateful to know.



    In a text file is a string like this:



    <span><span><span><span><span></span></span></span></span></span>


    The number of <span> and </span> varies from file to file. For example, in some files it is like this:



    <span></span>


    Yet in others it is like this:



    <span><span></span></span>


    And so on. There are likely never going to be more than 24 of each in a string.



    I want to eliminate all strings like this in the text file, yet preserve the </span> in strings like this:



    <span style="font-weight:bold;">text</span>


    There may be many variations on that kind of string in the text file; for example, <span style="font-size: 10px; font-weight: 400;">text</span> or <span style="font-size: 10px; font-weight: 400;">text</span> and I don't know beforehand what variation(s) will be included in the text file.



    This partially works...



    $original_file = 'in.txt'
    $destination_file = 'out.txt'

    (Get-Content $original_file) | Foreach-Object {
    $_ -replace '<span>', '' `
    -replace '</span>', ''
    } | Set-Content $destination_file


    ...but obviously results in something like <span style="font-weight:bold;">text.



    In the PowerShell script above I can use



        $_ -replace '<span></span>', '' `


    But of course it only catches the <span></span> in the middle of the string because, as it is written now, it does not loop.



    I know it is silly to do something like this



    $original_file = 'in.txt'
    $destination_file = 'out.txt'

    (Get-Content $original_file) | Foreach-Object {
    $_ -replace '<span></span>', '' `
    -replace '<span></span>', '' `
    -replace '<span></span>', '' `
    -replace '<span></span>', '' `
    -replace '<span></span>', ''
    } | Set-Content $destination_file


    So because the <span> string collapses into itself each time the script is run, producing a new inner <span></span> that can then be removed, the best solution I can think of is to loop the script over the file until it recognizes that all instances of <span></span> are gone.



    I feel like adding logic along these lines is necessary:



       foreach($i in 1..24){
    Write-Host $i


    But have not been able to successfully incorporate it into the script.



    If this is the wrong approach entirely I would be grateful to know.



    The reason for PowerShell is that my team prefers it for scripts included in an Azure DevOps release pipeline.



    Thanks for any ideas or help.










    share|improve this question


























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I am new at PowerShell and have not found a Stack Overflow question or a documentation reference that gets me all the way to a successful outcome. If a question or documentation reference already exists that answers this that I overlooked I would be grateful to know.



      In a text file is a string like this:



      <span><span><span><span><span></span></span></span></span></span>


      The number of <span> and </span> varies from file to file. For example, in some files it is like this:



      <span></span>


      Yet in others it is like this:



      <span><span></span></span>


      And so on. There are likely never going to be more than 24 of each in a string.



      I want to eliminate all strings like this in the text file, yet preserve the </span> in strings like this:



      <span style="font-weight:bold;">text</span>


      There may be many variations on that kind of string in the text file; for example, <span style="font-size: 10px; font-weight: 400;">text</span> or <span style="font-size: 10px; font-weight: 400;">text</span> and I don't know beforehand what variation(s) will be included in the text file.



      This partially works...



      $original_file = 'in.txt'
      $destination_file = 'out.txt'

      (Get-Content $original_file) | Foreach-Object {
      $_ -replace '<span>', '' `
      -replace '</span>', ''
      } | Set-Content $destination_file


      ...but obviously results in something like <span style="font-weight:bold;">text.



      In the PowerShell script above I can use



          $_ -replace '<span></span>', '' `


      But of course it only catches the <span></span> in the middle of the string because, as it is written now, it does not loop.



      I know it is silly to do something like this



      $original_file = 'in.txt'
      $destination_file = 'out.txt'

      (Get-Content $original_file) | Foreach-Object {
      $_ -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', ''
      } | Set-Content $destination_file


      So because the <span> string collapses into itself each time the script is run, producing a new inner <span></span> that can then be removed, the best solution I can think of is to loop the script over the file until it recognizes that all instances of <span></span> are gone.



      I feel like adding logic along these lines is necessary:



         foreach($i in 1..24){
      Write-Host $i


      But have not been able to successfully incorporate it into the script.



      If this is the wrong approach entirely I would be grateful to know.



      The reason for PowerShell is that my team prefers it for scripts included in an Azure DevOps release pipeline.



      Thanks for any ideas or help.










      share|improve this question















      I am new at PowerShell and have not found a Stack Overflow question or a documentation reference that gets me all the way to a successful outcome. If a question or documentation reference already exists that answers this that I overlooked I would be grateful to know.



      In a text file is a string like this:



      <span><span><span><span><span></span></span></span></span></span>


      The number of <span> and </span> varies from file to file. For example, in some files it is like this:



      <span></span>


      Yet in others it is like this:



      <span><span></span></span>


      And so on. There are likely never going to be more than 24 of each in a string.



      I want to eliminate all strings like this in the text file, yet preserve the </span> in strings like this:



      <span style="font-weight:bold;">text</span>


      There may be many variations on that kind of string in the text file; for example, <span style="font-size: 10px; font-weight: 400;">text</span> or <span style="font-size: 10px; font-weight: 400;">text</span> and I don't know beforehand what variation(s) will be included in the text file.



      This partially works...



      $original_file = 'in.txt'
      $destination_file = 'out.txt'

      (Get-Content $original_file) | Foreach-Object {
      $_ -replace '<span>', '' `
      -replace '</span>', ''
      } | Set-Content $destination_file


      ...but obviously results in something like <span style="font-weight:bold;">text.



      In the PowerShell script above I can use



          $_ -replace '<span></span>', '' `


      But of course it only catches the <span></span> in the middle of the string because, as it is written now, it does not loop.



      I know it is silly to do something like this



      $original_file = 'in.txt'
      $destination_file = 'out.txt'

      (Get-Content $original_file) | Foreach-Object {
      $_ -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', '' `
      -replace '<span></span>', ''
      } | Set-Content $destination_file


      So because the <span> string collapses into itself each time the script is run, producing a new inner <span></span> that can then be removed, the best solution I can think of is to loop the script over the file until it recognizes that all instances of <span></span> are gone.



      I feel like adding logic along these lines is necessary:



         foreach($i in 1..24){
      Write-Host $i


      But have not been able to successfully incorporate it into the script.



      If this is the wrong approach entirely I would be grateful to know.



      The reason for PowerShell is that my team prefers it for scripts included in an Azure DevOps release pipeline.



      Thanks for any ideas or help.







      regex powershell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 22:09









      LotPings

      15.5k61531




      15.5k61531










      asked Nov 10 at 17:41









      hcdocs

      696




      696
























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If you just want to remove any number of empty spans use a Regular Expression with a group and a quantifier:



          $original_file = 'in.txt'
          $destination_file = 'out.txt'

          (Get-Content $original_file) -replace "(<span>)+(</span>)+" |
          Set-Content $destination_file





          share|improve this answer




























            up vote
            0
            down vote













            Try the following .. i've added some comments to clearify things.



            # always use absolute paths if possible
            $original_file = 'c:tmpin.txt'
            $destination_file = 'c:tmpout.txt'

            $patternToBeRemoved = '<span></span>'

            # store the file contents in a variable
            $fileContent = Get-Content -Path $original_file

            # save the result of these operations in a new variable and iterate through each line
            $newContent = foreach($string in $fileContent) {
            # while the pattern you don't want is found it will be removed
            while($string.Contains($patternToBeRemoved)) {
            $string = $string.Replace($patternToBeRemoved, '')
            }
            # when it's no longer found the new string is returned
            $string
            }

            # save the new content in the destination file
            Set-Content -Path $destination_file -Value $newContent





            share|improve this answer




























              up vote
              0
              down vote













              $original_file = 'in.txt'
              $destination_file = 'out.txt'

              ForEach ($Line in (Get-Content $original_file)) {
              Do {
              $Line = $Line -replace '<span></span>',''
              } While ($Line -match '<span></span>')
              Set-Content -Path $destination_file -Value $Line
              }





              share|improve this answer




























                up vote
                0
                down vote













                You can use a regular expression together with the -replace operator to strip all <span>optional content</span> pairs from a string. That means all pairs where the opening tag does not specify any attributes.



                $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                $regex = '<span>(.*?)</span>'
                while ($content -match $regex)
                {
                $content = $content -replace $regex,'$1'
                }
                Write-Output $content


                The result will be:



                <span style="font-weight:bold;">Foo</span>


                The while loop takes care of your nested occurrences of the <span></span> pair.






                share|improve this answer






























                  up vote
                  0
                  down vote













                  $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                  $regex = '<spans+[^<]+</span>'
                  $null = $content -match $regex

                  $Matches[0]





                  share|improve this answer








                  New contributor




                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.


















                  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                    – Elletlar
                    Nov 11 at 0:19











                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241692%2floop-search-and-replace-two-part-string-over-file-using-powershell-while-preserv%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  1
                  down vote



                  accepted










                  If you just want to remove any number of empty spans use a Regular Expression with a group and a quantifier:



                  $original_file = 'in.txt'
                  $destination_file = 'out.txt'

                  (Get-Content $original_file) -replace "(<span>)+(</span>)+" |
                  Set-Content $destination_file





                  share|improve this answer

























                    up vote
                    1
                    down vote



                    accepted










                    If you just want to remove any number of empty spans use a Regular Expression with a group and a quantifier:



                    $original_file = 'in.txt'
                    $destination_file = 'out.txt'

                    (Get-Content $original_file) -replace "(<span>)+(</span>)+" |
                    Set-Content $destination_file





                    share|improve this answer























                      up vote
                      1
                      down vote



                      accepted







                      up vote
                      1
                      down vote



                      accepted






                      If you just want to remove any number of empty spans use a Regular Expression with a group and a quantifier:



                      $original_file = 'in.txt'
                      $destination_file = 'out.txt'

                      (Get-Content $original_file) -replace "(<span>)+(</span>)+" |
                      Set-Content $destination_file





                      share|improve this answer












                      If you just want to remove any number of empty spans use a Regular Expression with a group and a quantifier:



                      $original_file = 'in.txt'
                      $destination_file = 'out.txt'

                      (Get-Content $original_file) -replace "(<span>)+(</span>)+" |
                      Set-Content $destination_file






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 11 at 13:12









                      LotPings

                      15.5k61531




                      15.5k61531
























                          up vote
                          0
                          down vote













                          Try the following .. i've added some comments to clearify things.



                          # always use absolute paths if possible
                          $original_file = 'c:tmpin.txt'
                          $destination_file = 'c:tmpout.txt'

                          $patternToBeRemoved = '<span></span>'

                          # store the file contents in a variable
                          $fileContent = Get-Content -Path $original_file

                          # save the result of these operations in a new variable and iterate through each line
                          $newContent = foreach($string in $fileContent) {
                          # while the pattern you don't want is found it will be removed
                          while($string.Contains($patternToBeRemoved)) {
                          $string = $string.Replace($patternToBeRemoved, '')
                          }
                          # when it's no longer found the new string is returned
                          $string
                          }

                          # save the new content in the destination file
                          Set-Content -Path $destination_file -Value $newContent





                          share|improve this answer

























                            up vote
                            0
                            down vote













                            Try the following .. i've added some comments to clearify things.



                            # always use absolute paths if possible
                            $original_file = 'c:tmpin.txt'
                            $destination_file = 'c:tmpout.txt'

                            $patternToBeRemoved = '<span></span>'

                            # store the file contents in a variable
                            $fileContent = Get-Content -Path $original_file

                            # save the result of these operations in a new variable and iterate through each line
                            $newContent = foreach($string in $fileContent) {
                            # while the pattern you don't want is found it will be removed
                            while($string.Contains($patternToBeRemoved)) {
                            $string = $string.Replace($patternToBeRemoved, '')
                            }
                            # when it's no longer found the new string is returned
                            $string
                            }

                            # save the new content in the destination file
                            Set-Content -Path $destination_file -Value $newContent





                            share|improve this answer























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Try the following .. i've added some comments to clearify things.



                              # always use absolute paths if possible
                              $original_file = 'c:tmpin.txt'
                              $destination_file = 'c:tmpout.txt'

                              $patternToBeRemoved = '<span></span>'

                              # store the file contents in a variable
                              $fileContent = Get-Content -Path $original_file

                              # save the result of these operations in a new variable and iterate through each line
                              $newContent = foreach($string in $fileContent) {
                              # while the pattern you don't want is found it will be removed
                              while($string.Contains($patternToBeRemoved)) {
                              $string = $string.Replace($patternToBeRemoved, '')
                              }
                              # when it's no longer found the new string is returned
                              $string
                              }

                              # save the new content in the destination file
                              Set-Content -Path $destination_file -Value $newContent





                              share|improve this answer












                              Try the following .. i've added some comments to clearify things.



                              # always use absolute paths if possible
                              $original_file = 'c:tmpin.txt'
                              $destination_file = 'c:tmpout.txt'

                              $patternToBeRemoved = '<span></span>'

                              # store the file contents in a variable
                              $fileContent = Get-Content -Path $original_file

                              # save the result of these operations in a new variable and iterate through each line
                              $newContent = foreach($string in $fileContent) {
                              # while the pattern you don't want is found it will be removed
                              while($string.Contains($patternToBeRemoved)) {
                              $string = $string.Replace($patternToBeRemoved, '')
                              }
                              # when it's no longer found the new string is returned
                              $string
                              }

                              # save the new content in the destination file
                              Set-Content -Path $destination_file -Value $newContent






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 10 at 18:22









                              Guenther Schmitz

                              7431214




                              7431214






















                                  up vote
                                  0
                                  down vote













                                  $original_file = 'in.txt'
                                  $destination_file = 'out.txt'

                                  ForEach ($Line in (Get-Content $original_file)) {
                                  Do {
                                  $Line = $Line -replace '<span></span>',''
                                  } While ($Line -match '<span></span>')
                                  Set-Content -Path $destination_file -Value $Line
                                  }





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    $original_file = 'in.txt'
                                    $destination_file = 'out.txt'

                                    ForEach ($Line in (Get-Content $original_file)) {
                                    Do {
                                    $Line = $Line -replace '<span></span>',''
                                    } While ($Line -match '<span></span>')
                                    Set-Content -Path $destination_file -Value $Line
                                    }





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      $original_file = 'in.txt'
                                      $destination_file = 'out.txt'

                                      ForEach ($Line in (Get-Content $original_file)) {
                                      Do {
                                      $Line = $Line -replace '<span></span>',''
                                      } While ($Line -match '<span></span>')
                                      Set-Content -Path $destination_file -Value $Line
                                      }





                                      share|improve this answer












                                      $original_file = 'in.txt'
                                      $destination_file = 'out.txt'

                                      ForEach ($Line in (Get-Content $original_file)) {
                                      Do {
                                      $Line = $Line -replace '<span></span>',''
                                      } While ($Line -match '<span></span>')
                                      Set-Content -Path $destination_file -Value $Line
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 10 at 18:24









                                      ErikW

                                      947




                                      947






















                                          up vote
                                          0
                                          down vote













                                          You can use a regular expression together with the -replace operator to strip all <span>optional content</span> pairs from a string. That means all pairs where the opening tag does not specify any attributes.



                                          $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                          $regex = '<span>(.*?)</span>'
                                          while ($content -match $regex)
                                          {
                                          $content = $content -replace $regex,'$1'
                                          }
                                          Write-Output $content


                                          The result will be:



                                          <span style="font-weight:bold;">Foo</span>


                                          The while loop takes care of your nested occurrences of the <span></span> pair.






                                          share|improve this answer



























                                            up vote
                                            0
                                            down vote













                                            You can use a regular expression together with the -replace operator to strip all <span>optional content</span> pairs from a string. That means all pairs where the opening tag does not specify any attributes.



                                            $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                            $regex = '<span>(.*?)</span>'
                                            while ($content -match $regex)
                                            {
                                            $content = $content -replace $regex,'$1'
                                            }
                                            Write-Output $content


                                            The result will be:



                                            <span style="font-weight:bold;">Foo</span>


                                            The while loop takes care of your nested occurrences of the <span></span> pair.






                                            share|improve this answer

























                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              You can use a regular expression together with the -replace operator to strip all <span>optional content</span> pairs from a string. That means all pairs where the opening tag does not specify any attributes.



                                              $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                              $regex = '<span>(.*?)</span>'
                                              while ($content -match $regex)
                                              {
                                              $content = $content -replace $regex,'$1'
                                              }
                                              Write-Output $content


                                              The result will be:



                                              <span style="font-weight:bold;">Foo</span>


                                              The while loop takes care of your nested occurrences of the <span></span> pair.






                                              share|improve this answer














                                              You can use a regular expression together with the -replace operator to strip all <span>optional content</span> pairs from a string. That means all pairs where the opening tag does not specify any attributes.



                                              $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                              $regex = '<span>(.*?)</span>'
                                              while ($content -match $regex)
                                              {
                                              $content = $content -replace $regex,'$1'
                                              }
                                              Write-Output $content


                                              The result will be:



                                              <span style="font-weight:bold;">Foo</span>


                                              The while loop takes care of your nested occurrences of the <span></span> pair.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 10 at 18:49

























                                              answered Nov 10 at 18:19









                                              Manuel Batsching

                                              724312




                                              724312






















                                                  up vote
                                                  0
                                                  down vote













                                                  $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                                  $regex = '<spans+[^<]+</span>'
                                                  $null = $content -match $regex

                                                  $Matches[0]





                                                  share|improve this answer








                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.


















                                                  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                    – Elletlar
                                                    Nov 11 at 0:19















                                                  up vote
                                                  0
                                                  down vote













                                                  $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                                  $regex = '<spans+[^<]+</span>'
                                                  $null = $content -match $regex

                                                  $Matches[0]





                                                  share|improve this answer








                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.


















                                                  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                    – Elletlar
                                                    Nov 11 at 0:19













                                                  up vote
                                                  0
                                                  down vote










                                                  up vote
                                                  0
                                                  down vote









                                                  $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                                  $regex = '<spans+[^<]+</span>'
                                                  $null = $content -match $regex

                                                  $Matches[0]





                                                  share|improve this answer








                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  $content = '<span></span><span><span><span style="font-weight:bold;">Foo</span></span></span>'
                                                  $regex = '<spans+[^<]+</span>'
                                                  $null = $content -match $regex

                                                  $Matches[0]






                                                  share|improve this answer








                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  share|improve this answer



                                                  share|improve this answer






                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.









                                                  answered Nov 11 at 0:13









                                                  walid

                                                  1




                                                  1




                                                  New contributor




                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.





                                                  New contributor





                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.






                                                  walid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                  Check out our Code of Conduct.












                                                  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                    – Elletlar
                                                    Nov 11 at 0:19


















                                                  • Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                    – Elletlar
                                                    Nov 11 at 0:19
















                                                  Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                  – Elletlar
                                                  Nov 11 at 0:19




                                                  Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.How to Answer
                                                  – Elletlar
                                                  Nov 11 at 0:19


















                                                   

                                                  draft saved


                                                  draft discarded



















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241692%2floop-search-and-replace-two-part-string-over-file-using-powershell-while-preserv%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Xamarin.iOS Cant Deploy on Iphone

                                                  Glorious Revolution

                                                  Dulmage-Mendelsohn matrix decomposition in Python