代碼
$original = 'This_is my code [email protected]# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'''
$desired = 'This_is-my-code-----characters-are-not---allowed--remove-spaces-----------_--------'
$replacements = "[^a-zA-Z_]" # anything that's _not_ a-z or underscore
$result = $original -replace $replacements, '-'
Write-Host "Original: $c"
Write-Host "Desired : $d"
Write-Host "Result : $r"
結果
Original: This_is my code [email protected]# characters are not $ allowed% remove spaces ^&*(){}[]/_:;,.?/"'
Desired : This_is-my-code-----characters-are-not---allowed--remove-spaces-----------_--------
Result : This_is-my-code-----characters-are-not---allowed--remove-spaces-----------_--------
定義特殊。你想要什麼角色?只是a-z,A-Z,0-9和連字符? –
所以輸出應該是這樣的 - 'This_is-my-code ----- characters-are-not --- allowed - remove-spaces -----------_---- ----' –
無論哪裏有空間,我都想替換連字符。無論哪裏有特殊的角色,我都想用空間替換。空間將再次被替換爲連字符。 – user2598808