Replace multiline text in a file using Powershell without using Regex

前端 未结 3 1619
广开言路
广开言路 2021-01-14 03:49

I have the following Powershell script:

$oldCode =  @\"
            
3条回答
  •  爱一瞬间的悲伤
    2021-01-14 04:38

    For Pete's sake, don't even think about using regex for HTML.

    The problem you met is that reading a file will provide you an array of strings. Replace() doesn't know about arrays, so you got to work it by hand. You could create a big string with -join like so,

    $fileContent = [System.Io.File]::ReadAllText($_.FullName)
    $theOneString = $fileContent -join ' '
    $theOneString.Replace($foo, $bar)
    

    ... But this will mess up your line breaks. Then again, you could reformat the string with HTML Tidy.

    The manual way is to iterate the source array row by row. Until you find the

    , copy the contents into new destination array. After finding the replacable part, insert rest of the new stuff into the destination array. Keep reading and discarding the source array untill you find the
    and copy all the rest into the destination array. Finally save the destination array's contents and you are done.

提交回复
热议问题