从字符串

时间:2017-04-14 14:47:56

标签: powershell powershell-v5.0

我有一个xlsx文件,其中包含条形码列表,这些条形码在单元格中列出三个或四个,我需要将其拆分,因此我只有条形码。

条形码本身总是6个数字的字符串,但它们可能以几个不同的字母开头,并且在单元格中可能有也可能没有逗号,&符号和其他单词。它看起来像这样:

COL 1 | COL 2 | COL 3 | COL 4 | COL 5
Info  | Identifier | Info  | Info  | L123456 , PC 654321 , M 123654 & 546123 Vacant |
Info | Identifier | Info | Info | PC 123456 , M 456789 Occupied
Info | Identifier | Info | Info | L 987654

到目前为止,我已经尝试使用正则表达式来删除所有噪声数据,只留下条形码,但这已经回归了混乱。

我还需要有一种方法来跟踪它们来自哪一行,因为早期列中有一个标识符需要链接到这些条形码。我可以很容易地访问这个标识符。

我使用excel ComObject来操作此工作表。这是我用来尝试正则表达式的代码,我该如何提取条形码?

$xl = new-object -ComObject excel.application
$xl.visible = $true
$xl.displayalerts = $false
$xl.workbooks.open("file.xls")
$sheet = $xl.activeworkbook.activesheet
$x = 3
3..8|%{
    $uc = $sheet.Range("B"+$x).Text
    $equip = $sheet.Range("I"+$x).Text
    $loc = $sheet.Range("D"+$x).Text + '-NHQ'
    $uidcc = $uc.replace(" / ",",")
    $tagnums = $equip -replace " [A-Z]+ ",""
    $tagnums = $tagnums -replace " & ",""
    $tagnums = $tagnums -replace "[A-C][1-9]+",""
    $tagnums = $tagnums -split ','
    foreach($i in $tagnums){
        $asset += $i+","+$loc+","+$uidcc+"`n"
    }
    $x++
}
$asset | Format-Table
$xl.quit()
[System.Runtime.InteropServices.Marshal]::ReleaseComObject($xl)

1 个答案:

答案 0 :(得分:1)

如果我理解你,那么你需要这样的东西:

$tagnums = @([regex]::matches($equip,'\D*(\d{6})')|%{$_.groups[1].value})

例如,输入数据'L123456 , PC 654321 , M 123654 & 546123 Vacant'将是下一个输出:

123456
654321
123654
546123

'L 987654'将为987654

相关问题