Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell,将点替换为空格_Powershell_Character Replacement - Fatal编程技术网

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
    是正则表达式,用于任何可能重复的字符