选择字符串问题间歇性地工作
我有一些有时可以工作的代码,而有些则没有.当它失败时,我看不到任何错误,所以我为什么会断断续续地坚持下去.
I have some code that works sometimes and others not. I see no errors when it fails so I am stuck as to why its intermittent.
我正在使用Select-String cmdlet,它将找到许多我想要的匹配项.
I am using the Select-String cmdlet which will find many matches which is what I want.
我还使用了与测试相同的文本文件,因此它所搜索的数据不会更改.
I am also using the same text file as my test so its not the data being searched changing.
$Hospinput.Text
是输入要搜索的项目.
$Hospinput.Text
is the item being input to search for.
搜索文件的内容为:
Windows 7 Clinical Complete Nursing A3S
Windows 7 Clinical Complete Nursing A3S Wireless
Windows 7 Clinical Complete Nursing A4N
Windows 7 Clinical Complete Nursing A4N Wireless
Windows 7 Clinical Complete Nursing A4S
Windows 7 Clinical Complete Nursing A4S Wireless
Windows 7 Clinical Complete Observation
Windows 7 Clinical Complete Observation Wireless
SPU Fastpass
SPU Fastpass Wireless
24-7 - Windows 7 Pro x86
Admitting General - Windows 7 x86 - v1.7
通过在搜索框中输入胜利,它将显示此内容.任何带有胜利"的内容.
By entering win into the search box it will display this.. Anything with 'win' in it.
Windows 7 Clinical Complete Nursing A3S
Windows 7 Clinical Complete Nursing A3S Wireless
Windows 7 Clinical Complete Nursing A4N
Windows 7 Clinical Complete Nursing A4N Wireless
Windows 7 Clinical Complete Nursing A4S
Windows 7 Clinical Complete Nursing A4S Wireless
Windows 7 Clinical Complete Observation
Windows 7 Clinical Complete Observation Wireless
24-7 - Windows 7 Pro x86
Admitting General - Windows 7 x86 - v1.7
..此行上方没有代码,只有文本文件内容以及搜索win后应显示的内容.
..There is no code above this line only text file contents and what should be displayed after searching for win.
$list = (Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern $HospInput.Text | Select line | ft -HideTableHeaders | Out-String).Trim()
$separator = "`n`r"
$Array = @($list.split($separator, [System.StringSplitOptions]::RemoveEmptyEntries))
$Array.Length
If ($list)
{
$Array.trim()
$Array | foreach{$textbox1.Items.Add($_) }
}Else
{
$TextBox1.Text = "Error in finding $($hospInput.Text)"
}
上面的代码是表单的一部分.它将搜索文本文件并根据输入进行匹配.这很好用.但只有有时候.要以表格形式显示结果,请单击搜索按钮.当我单击搜索时,有时会显示结果.有时事实并非如此.而且每次测试我都没有做任何改变.
The code above is part of a form. It will search a text file and match based on input. This works great. But only sometimes. To display the results in the form i click a search button. It's when i click search sometimes it displays the results. sometimes it does not. And i am doing nothing different each time i test.
任何人都可以照亮吗?
can anyone shed any light?
它间歇性地完成
您不应在此处使用Format-Table
cmdlet,只需使用Select-Object
cmdlet上的-expand
开关来检索所需的输出:>
You shouldn't use the Format-Table
cmdlet here, just use the -expand
switch on the Select-Object
cmdlet to retrieve your desired output:
$list = Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) | Select -expand line
此外,您的问题可能与以下事实有关:Select-String
cmdlet 正在使用正则表达式,并且您可能在搜索框中输入了一些正则表达式字符?您可以通过在cmdlet中添加-SimpleMatch
开关来更改此行为:
Beside that, your issue is probably related to the fact, that the Select-String
cmdlet is using regex and you probably enter some regex characters into the searchbox? You could change this behaviour by adding the -SimpleMatch
switch to the cmdlet:
-SimpleMatch
-SimpleMatch
使用简单匹配而不是正则表达式匹配.在一个 简单匹配,Select-String在输入中搜索 模式参数中的文本.它不解释 模式参数作为正则表达式语句.
Uses a simple match rather than a regular expression match. In a simple match, Select-String searches the input for the text in the Pattern parameter. It does not interpret the value of the Pattern parameter as a regular expression statement.
您可以将代码重构为:
Select-String -AllMatches -Path "x:\Scripts\PowerShell\HospDepartments.txt" -pattern ($HospInput.Text) |
Select-Object -expand line | ForEach-Object {
$textbox1.Items.Add($_)
}