[英]How to replace character in Powershell with initial character + a space
[英]Powershell, replace DOT with SPACE
这有效:
$string = "This string, has a, lot, of commas in, it."
$string -replace ',',''
输出:该字符串中包含很多逗号。
但这不起作用:
$string = "This string. has a. lot. of dots in. it."
$string -replace '.',''
输出:空白。
为什么?
-replace
使用正则表达式(regexp)进行搜索,而在regexps中,点是特殊字符。 使用' \\
'转义它,它应该可以工作。 请参阅Get-Help about_Regular_Expressions
。
-replace
的第一个参数是正则表达式(但第二个参数不是) '.'
是正则表达式中的特殊字符,表示每个字符 $string -replace '.', ''
意思是:将每个字符替换为''
(空白字符) .
并将其视为普通字符,您必须使用\\
对其进行转义 $string -replace '\\.', ''
$string = $string -replace '\\.', ''
因此应该是:
$string = "This string. has a. lot. of dots in. it."
$string = $string -replace '\.', ''
接着
echo $string
结果是:
This string has a lot of dots in it
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.