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

In Powershell, how can I replace a string containing a question mark? For instance:

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

The question mark seems to be interpreted as a special character. Is there any way to avoid this? I tried using one or two backticks, but did not succeed.

+4
source share
1 answer

The operator -replaceuses regular expression pattern matching. In RegEx, the question mark is a quantifier indicating that the previous match must be zero or one time. You can avoid the question mark by preceding it with a backslash as such:

(Get-Content file.txt) -replace "Hello\?","Hello."
+6
source

Source: https://habr.com/ru/post/1688430/


All Articles