查找 Regex 在 Powershell 中是否不匹配



我有一个Powershell脚本,我用它来对包含多个条形码列表(以6个数字的字符串格式)和用户信息的列表的word文档集合中的信息重新排序到一个电子表格中,将任何具有未知条形码的用户重新排序到另一个电子表格中。我想添加一行,将任何没有条形码的用户也包括在未知条形码文件中,因为目前这些用户在处理中丢失了。

我有一个用于挑选条形码的正则表达式,并且我尝试检查$matches$matches变量中是否有 -eq '' 或 $matches -eq $null,它们分别返回工作表上的无输出或每个用户。我还尝试检查我存储条形码的变量是否$null或空,结果相同。最后,我尝试编写另一个正则表达式来检查条形码的格式是否不匹配,这返回了工作表上的每个用户。

以下是我正在使用的字段及其属性的示例:

#This is the field which would contain barcodes. Note that there are several instances
#of fields with this name in any given sheet. The text is stored in the Result property,
#and for this field it should include a listing of 6-digit numbers possibly including 
#letters to indicate the type of equipment the barcode represents. It will sometimes
#contain notes from whomever filled the sheet in.
Application     : Microsoft.Office.Interop.Word.ApplicationClass
Creator         : **REDACTED**
Parent          : Microsoft.Office.Interop.Word.DocumentClass
Type            : 70
Name            : Text10
EntryMacro      : 
ExitMacro       : 
OwnHelp         : False
OwnStatus       : False
HelpText        : 
StatusText      : 
Enabled         : True
Result          : EQUIPMENT WAS ALREADY MOVED
TextInput       : System.__ComObject
CheckBox        : System.__ComObject
DropDown        : System.__ComObject
Next            : System.__ComObject
Previous        : System.__ComObject
CalculateOnExit : False
Range           : System.__ComObject
------------------------------------------------
Application     : Microsoft.Office.Interop.Word.ApplicationClass
Creator         : **REDACTED**
Parent          : Microsoft.Office.Interop.Word.DocumentClass
Type            : 70
Name            : Text10
EntryMacro      : 
ExitMacro       : 
OwnHelp         : False
OwnStatus       : False
HelpText        : 
StatusText      : 
Enabled         : True
Result          : 
TextInput       : System.__ComObject
CheckBox        : System.__ComObject
DropDown        : System.__ComObject
Next            : System.__ComObject
Previous        : System.__ComObject
CalculateOnExit : False
Range           : System.__ComObject
-----------------------------------------------
Application     : Microsoft.Office.Interop.Word.ApplicationClass
Creator         : **REDACTED**
Parent          : Microsoft.Office.Interop.Word.DocumentClass
Type            : 70
Name            : Text10
EntryMacro      : 
ExitMacro       : 
OwnHelp         : False
OwnStatus       : False
HelpText        : 
StatusText      : 
Enabled         : True
Result          : L 123456, M 654321, 456789, D 987654
TextInput       : System.__ComObject
CheckBox        : System.__ComObject
DropDown        : System.__ComObject
Next            : System.__ComObject
Previous        : System.__ComObject
CalculateOnExit : False
Range           : System.__ComObject

这是我正在使用的代码(除了让用户没有条形码之外,它运行良好),包括我最近挑选这些用户的失败尝试:

$word = New-Object -ComObject Word.Application
$word.Visible = $false
$word.DisplayAlerts = "wdAlertsNone"
$results = "Header1,Header2,Header3,Header4`n"
$exceptions = "Header2,Header3,Header4`n"
$list = get-childitem 'H:directory'
foreach($file in $list){
$path = "H:directory$($file.name)"
$doc = $word.Documents.Open($path)
$fields = $doc.FormFields
foreach($field in $fields){
if ($field.name -eq 'Text2'){
$ucc = $field.Result.Replace(' / ',",")
$ucc = $ucc.Replace('/',',')
}
elseif($field.name -eq 'Text5'){
$loc = $field.Result
}
$loc = $loc.Trim()
}
elseif($field.name -eq 'Text10'){
if($field.result -like '*UNK*'){
$exceptions+=$loc + "," + $ucc+"`n"
}
#this is where I am storing the barcodes
$bar = @([regex]::matches($field.Result,'D*(d{6})')|%{$_.groups[1].value})
#this is my attempt at picking out blank codes
if([regex]::Match($field.result,"*(d[6])*") -ne $true){
$exceptions+=$loc + "," + $ucc + "`n"
}
foreach($code in $bar){
$results += $code + "," + $loc + "," + $ucc+"`n"
}
}
}
}
$exceptions | out-file 'H:directoryexceptions.csv' -Encoding ascii -Force
$results | out-file 'H:directorylist.csv' -Encoding ascii -Force
$word.quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)

你可以稍微重组一下事情,用管道连接到ForEach循环的Where语句,并利用自动变量$Matches

$bar = $field.Result | Where{$_ -match 'D*(d{6})'} | ForEach{$Matches[1].value}
If(!$bar -eq $null){
foreach($code in $bar){
$results += $code + "," + $loc + "," + $ucc+"`n"
}
}Else{
$exceptions+=$loc + "," + $ucc + "`n"
}

编辑:我添加了一些对象来模拟您的示例数据,对于每个对象,我为$loc$ucc变量生成一些随机字母。然后我只是输出到屏幕而不是文件。这是我正在查看的内容:

$letters= 97..123|%{[char]$_}
$fields = @(
[pscustomobject]@{'Name' = 'Text10';'Result' = @('asdf123456','dasrew345678','fdswer098765')},
[pscustomobject]@{'Name' = 'Text10';'Result' = 'EQUIPMENT WAS ALREADY MOVED'},
[pscustomobject]@{'Name' = 'Text10';'Result' = ''}
)
foreach($field in $fields){
$loc = (Get-Random -Count 5 -InputObject $letters) -join ''
$ucc = (Get-Random -Count 5 -InputObject $letters) -join ''
if ($field.name -eq 'Text2'){
$ucc = $field.Result.Replace(' / ',",")
$ucc = $ucc.Replace('/',',')
}
elseif($field.name -eq 'Text5'){
$loc = $field.Result
#}
$loc = $loc.Trim()
}
elseif($field.name -eq 'Text10'){
if($field.result -like '*UNK*'){
$exceptions+=$loc + "," + $ucc+"`n"
}
$bar = $field.Result | Where{ $_ -match 'D*(d{6})' } | ForEach {$Matches[1]}
If(!($bar -eq $null)){
foreach($code in $bar){
"results+=$code + `",`" + $loc + `",`" + $ucc"
}
}Else{
"exceptions+=$loc + `",`" + $ucc"
}
}
}

输出 5 行。三个用于第一个对象,所有对象用于相同的"用户",loc 和 ucc 使用相同的 5 个字符,然后每行一个条形码。然后它为没有条形码的记录再输出 2 行。

results+=123456 + "," + vpsjt + "," + lmsyv
results+=345678 + "," + vpsjt + "," + lmsyv
results+=098765 + "," + vpsjt + "," + lmsyv
exceptions+=hbptn + "," + ugydn
exceptions+=ygbop + "," + zy{mt

最新更新