Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Powershell, how can I replace a string that contains a question mark?

In Powershell, how can I replace a string that contains a question mark? For example:

(Get-Content file.txt) -replace "Hello?","Hello."

The question mark seems to be interpreted as some kind special character. Is there a way to escape it? I tried using one or two backticks, but no success.

like image 458
Stever Avatar asked Oct 31 '17 00:10

Stever


1 Answers

The -replace operator uses Regular Expression pattern matching. In RegEx the question mark is a quantifier indicating the previous match should be matched zero or one times. You can escape the question mark by placing a backslash before it as such:

(Get-Content file.txt) -replace "Hello\?","Hello."
like image 137
TheMadTechnician Avatar answered Nov 19 '22 06:11

TheMadTechnician