如何根据一组固定的字符单独循环从文件中读取的字符串,以及它们是否匹配,在powershell中打印字符串



我目前有一个foreach循环,它从一个小字典文件中获取内容(只有超过3个字符的字符串(。我希望将$行中的每个字符与我的其他字符进行比较,在这种情况下;b"i〃"n〃"g"o";这样,如果$line中的所有字符都在宾果游戏中,那么它就会打印单词。否则循环到下一个单词
到目前为止,我有:

foreach($line in Get-Content Desktop/dict.txt | Sort-Object Length, { $_ })

我不能得到的一点(对powershell不太熟悉(是:

if($line.length -gt 3){
if( i in $line == 'b')
if( i in $line == 'i')
if( i in $line == 'n')
if( i in $line == 'g')
if( i in $line == 'o')
write-output $line
}
}

如果我理解正确,如果您想检查bing中是否包含$line,则可以将-match用于不区分大小写的运算符,将-cmatch用于区分大小写运算符。请参见比较运算符。

例如:

PS /> 'bingo' -match 'ing'
True
PS /> 'bingo' -match 'bin'
True
PS /> 'bingo' -match 'ngo'
True

代码可能看起来像这样:

foreach($line in Get-Content Desktop/dict.txt | Sort-Object Length, { $_ })
{
if($line.length -gt 3 -and 'bingo' -match $line)
{
$line
# you can add break here to stop this loop if the word is found
}
}

编辑

如果你想检查bingo中的3个或更多字符(按任何顺序(是否包含在$line中,有很多方法可以做到这一点,我会采用以下方法:

# Insert magic word here
$magicWord = 'bingo'.ToCharArray() -join '|'
foreach($line in Get-Content Desktop/dict.txt | Sort-Object Length, { $_ })
{
# Remove [Text.RegularExpressions.RegexOptions]::IgnoreCase if you want it to be Case Sensitive
$check = [regex]::Matches($line,$magicWord,[Text.RegularExpressions.RegexOptions]::IgnoreCase)
# If 3 or more unique characters were matched
if(($check.Value | Select-Object -Unique).count -ge 3)
{
'Line is: {0} || Characters Matched: {1}' -f $line,-join $check.Value 
}
}

Demo

给定以下单词:

$test = 'ngob','ibgo','gn','foo','var','boing','ingob','oubingo','asdBINGasdO!'

它将产生:

Line is: ngob || Characters Matched: ngob
Line is: ibgo || Characters Matched: ibgo
Line is: boing || Characters Matched: boing
Line is: ingob || Characters Matched: ingob
Line is: oubingo || Characters Matched: obingo
Line is: asdBINGasdO! || Characters Matched: BINGO

所以您想取回任何长度相同、字符相同的单词,无论顺序如何?

$dict = @(
'bingo'
'rambo'
'big'
'gobin'
'bee'
'ebe'
'been'
'ginbo'
)
$word = 'bingo'
$dict |
Where-Object { $_.length -eq $word.Length } |
ForEach-Object {
$dictwordLetters = [System.Collections.Generic.List[char]]::new($_.ToCharArray())
$word.ToCharArray() | ForEach-Object {
$dictwordLetters.Remove($_) | Out-Null
}
if (-not $dictwordLetters.Count) {
$_
}
}

以下将是输出

bingo
gobin
ginbo

通过两个答案的一部分,我得到了我想要的结果。由于我是新手,不知道如何感谢@martin和@santiago的工作。

这是组合在一起的代码,它几乎占用了字典文件,然后不是固定的字符串大小,而是使其大于3:

$dict = @(Get-Content Desktop/dict.txt | Sort-Object Length, { $_ })
$word = 'bingo'
$dict |
Where-Object { $_.length -gt 2 } |
ForEach-Object {
$dictwordLetters = [System.Collections.Generic.List[char]]::new($_.ToCharArray())
$word.ToCharArray() | ForEach-Object {
$dictwordLetters.Remove($_) | Out-Null
}
if (-not $dictwordLetters.Count) {
$_
}
}

非常感谢您的协助。

这是我的两分钱:

$dict = 'apple', 'brown', 'green', 'cake', 'bin', 'pear', 'big', 'milk', 'bio', 'bong', 'bingo', 'bodings', 'gibson'
# the search term as string
$term = 'bingo'
# merge the unique characters into a regex like '[bingo]+'
$chars = '[{0}]+' -f (($term.ToCharArray() | Select-Object -Unique) -join '')
# loop over the array (lines in the text file)
$dict | ForEach-Object {
# get all matched characters, uniqify and join if there are more matches. 
$found = (($_ | Select-String -Pattern $chars -AllMatches).Matches.Value | Select-Object -Unique ) -join '' | Where-Object { $_.Length -ge 3 }
if ($found) { 
# outputting an object better explains what is matched in which line
[PsCustomObject]@{
Line              = $_
CharactersMatched = $found
}
# of course, you can also simply output the found matching characters
# $found
}
}

输出:

Line    CharactersMatched
----    -----------------
brown   bon              
bin     bin              
big     big              
bio     bio              
bong    bong             
bingo   bingo            
bodings boing            
gibson  gibon 

前面的答案似乎都过于复杂。如果您试图匹配字符串,那么这听起来像是一个需要正则表达式的问题,如果是这样的话,那么Select-String将是比Get-Content更好的选择。以下是一个例子,我不确定它是否完全适合您的需求,但应该为您指明正确的方向:

Select-String 'Desktop/dict.txt' -pattern '^[bingo]{3,}$'

最新更新