Powershell,将点替换为空格
这项工作:Powershell,将点替换为空格,powershell,character-replacement,Powershell,Character Replacement,这项工作: $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中,点是一个特殊字符。使用“
$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中,点是一个特殊字符。使用“\
”对其进行转义,它应该可以工作。看
-replace
中的第一个参数是正则表达式(但第二个不是)。
是正则表达式中的一个特殊字符,表示每个字符$string-replace.”,“
表示:将每个字符替换为”
(空白字符)结果是得到一个空白字符串
因此,为了转义正则表达式特殊字符
并将其视为普通字符,必须使用\
$string-替换“\”,“
$string=$string-替换'\.',''
$string = "This string. has a. lot. of dots in. it."
$string = $string -replace '\.', ''
然后
echo$string
结果:
这个字符串中有很多点
它将其解析为正则表达式,afaik
是正则表达式,用于任何可能重复的字符