Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stanislavhordiyenko/5f0edd8ccded5bc442c7566895690759 to your computer and use it in GitHub Desktop.
Save stanislavhordiyenko/5f0edd8ccded5bc442c7566895690759 to your computer and use it in GitHub Desktop.
Replace text in multi line string in PowerShell. Below you can find several powershell examples how to replace text in a multi line string with regular expressions.
$text = "
This is
__
a multi line
string
__
with some marks in it.
"
# Here we remove a text in the multiline string between two marks
$text = $text -Replace "(?ms)__.*?__`n?`n?", ""
# We use Trim() method to remove new line from the beginning and the end of the string
Write-Output $text.Trim()
$text = "
This is
__
a multi line
string
__
with some marks in it.
"
# Here we remove marks in the multiline string
$text = $text -Replace "(?ms)^__`n?`n?", ""
# We use Trim() method to remove new line from the beginning and the end of the string as well
Write-Output $text.Trim()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment