【发布时间】:2017-09-22 21:17:45
【问题描述】:
我有一个 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:\directory\exceptions.csv' -Encoding ascii -Force
$results | out-file 'H:\directory\list.csv' -Encoding ascii -Force
$word.quit()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($word)
【问题讨论】:
-
请提供示例输入和所需输出以获得完整的minimal reproducible example。也许考虑格式化(例如换行符、段落)以使问题更具可读性。
-
"*(\d[6])*"看起来像匹配“任意数量的绝对没有(可能是语法错误),然后是任意数量的捕获的数字和 6 组”。正则表达式有什么不寻常的地方吗?
标签: regex powershell powershell-5.0