「PowerShell」正規表現式で文字を置き換えるサンプル

書式
“文字列" -replace 正規表現式 , 置換後の文字列
正規表現式を利用して文字を置き換えます。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# 正規表現 「a」または「k」
$target = "arkgame study" -replace "[ak]", "日"
Write-Host $target
# 正規表現 0から9の4桁
$target = "テスト23456789abc" -replace "[0-9]{4}", "###"
Write-Host $target
# 正規表現 aからzの4桁
$target = "studyskill" -replace "[a-z]{5}", "東京"
Write-Host $target
# 正規表現 「a」または「k」 $target = "arkgame study" -replace "[ak]", "日" Write-Host $target # 正規表現 0から9の4桁 $target = "テスト23456789abc" -replace "[0-9]{4}", "###" Write-Host $target # 正規表現 aからzの4桁 $target = "studyskill" -replace "[a-z]{5}", "東京" Write-Host $target
# 正規表現 「a」または「k」
$target = "arkgame study" -replace "[ak]", "日"

Write-Host $target 

# 正規表現 0から9の4桁
$target = "テスト23456789abc" -replace "[0-9]{4}", "###"

Write-Host $target

# 正規表現 aからzの4桁
$target = "studyskill" -replace "[a-z]{5}", "東京"

Write-Host $target

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
PS C:\study\powershell> .\test.ps1
日r日g日me study
テスト######abc
東京東京
PS C:\study\powershell> .\test.ps1 日r日g日me study テスト######abc 東京東京
PS C:\study\powershell> .\test.ps1
日r日g日me study
テスト######abc
東京東京

 

PowerShell

Posted by arkgame