【发布时间】:2020-09-11 07:15:49
【问题描述】:
我目前正在尝试匹配一种 ID 模式并替换为 0 或 1。
example pc0045601234 replace with 1234 the last 4 and add the 3rd digit in front "01234"
我尝试了下面的代码,但结果只用没有匹配的员工填充了 userid 列
$reportPath = '.\report.csv'`$reportPath = '.\report.csv'`
$csvPath = '.\output.csv'
$data = Import-Csv -Path $reportPath
$output = @()
foreach ($row in $data) {
$table = "" | Select ID,FirstName,LastName,userid
$table.ID = $row.ID
$table.FirstName = $row.FirstName
$table.LastName = $row.LastName
switch -Wildcard ($row.ID)
{
{$row.ID -match 'P\d\d\d\d\d\D\D\D'} {$table.userid = "Contractor"; continue}
{$row.ID -match 'SEC\d\d\d\D\D\D\D'} {$table.userid = "Contractor"; continue}
{$row.ID.StartsWith("P005700477")} {$table.userid = $row.ID -replace "P005700477","0477"; continue}
{$row.ID.StartsWith("P00570")} {$table.userid = $row.ID -replace "P00570","0"; continue}
default {$table.userid = "No Matching Employee"}
}
$output += $table
}
$output | Export-csv -NoTypeInformation -Path $csvPath
【问题讨论】:
-
所以你从 pc0045601234 开始,想以什么结束?
-
我想得到 5 个数字。无论第三个数字是 0 还是 1 以及最后 4 个数字。例如 PC05601234 为 01234 或 PC15601234 为 11234
标签: regex powershell-4.0