匹配我有我用它来重新排序包含多个条形码的列表(6个的一串数字的格式),Word文档和用户信息的收集到的信息的PowerShell脚本一个电子表格和任何有未知条码的用户进入另一个。我想添加一行以将任何没有条形码的用户包括到未知的条形码文件中,因为当前这些用户正在处理中丢失。找到,如果正则表达式是不是在PowerShell中
我有一个正则表达式,我用来挑出条形码,并且我已经尝试检查$ 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)
请提供完整[mcve]的样本输入和所需输出。也许考虑格式化(例如换行符,段落)以使问题更具可读性。 – Yunnosch
'“*(\ d [6])*”'看起来像匹配“任意数量绝对没有的(可能语法错误),随后通过任何数量的数字和图6的捕获基团的”。对正则表达式有什么不寻常的地方吗? – Yunnosch
让我们[继续聊天讨论](http://chat.stackoverflow.com/rooms/142654/discussion-between-yunnosch-and-cameron)。 – Yunnosch