2013-07-18 92 views
0

這是我的數組:如何在字符串中插入0

eth1 
eth12 
eth5 
eth11 

當我的字符串中包含1和9之間的數字,我想前加一個0,以獲得一個這樣的數組:

eth01 
eth12 
eth05 
eth11 

我該如何做到這一點?我不知道如何修改像這樣的字符串:/

感謝

回答

9

像這樣:

$if = 'eth1', 'eth12', 'eth5', 'eth11' 
$if -replace '(\D+)(\d)$', '${1}0$2' 
+0

太棒了,謝謝 –

1

比安斯加爾的:)

[email protected]("eth1","eth12","eth5") 

$a|%{ 
    $slice=$_.split("h"); 
    if([int]$slice[1] -le 9){ 
     $slice[1]="0"+$slice[1] 
    } 
join-string -strings $slice -separator("h") 
} 
4

另一種選擇更曲折的方式:

$if -replace '(?<=\D)(\d)$', '0$1'