use*_*808 -1 regex string powershell replace special-characters
我想用连字符替换字符串中的任何特殊字符和空格。下面是我的代码:
$c = 'This_is my code !@# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'''
$c = $c -replace [regex]::Escape('!@#$%^&*(){}[]/:;,.?/"'),('-')
Write-Host $c
Run Code Online (Sandbox Code Playgroud)
有没有直接的方法来查找所有特殊字符、空格并替换为单个字符连字符
\W 将替换任何非单词字符。它不会取代a-z, A-Z, 0-9
$c = 'This_is my code !@# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'''
$c -replace '\W','-'
This_is-my-code-----characters-are-not---allowed--remove-spaces-----------_--------
Run Code Online (Sandbox Code Playgroud)