使用 powershell 查找与两个单独的正则表达式匹配的文件 [英] Using powershell to find files that match two seperate regular expressions
问题描述
我想找到任何可以匹配两个正则表达式的文件,例如所有文件至少具有bat
、hat
、cat
以及词名词
.这将是可以匹配正则表达式 [bhc]at
和 noun
的任何文件.
I want to find any file that can match two regular expressions, for example all files that have at least one of bat
, hat
, cat
as well as the word noun
. That would be any file that can match both regular expressions [bhc]at
and noun
.
select-string
的正则表达式参数似乎只能逐行工作.似乎您可以传入以逗号分隔的多个模式(select-string -pattern "[bhc]at","noun"
),但它匹配一个,而不是两个.
The regex parameter to select-string
only seems to work on a line-by-line basis. It seems you can pass in multiple patterns delimited by commas (select-string -pattern "[bhc]at","noun"
) but it matches either, rather than both.
推荐答案
将@mjolinor 的单个 get-content 与@JNK 的优化过滤合并提供:
Merging @mjolinor's single get-content with @JNK's optimised filtering gives:
dir | ?{ [string](gc $_) | ?{$_ -match "[bhc]at"} | ?{$_ -match "noun"} }
如果您不介意重复,您可以通过管道将结果选择字符串以查看这些匹配的上下文:
If you don't mind the repetition, you can pipe the results to select string to view the contexts of these matches:
| select-string -pattern "[bhc]at","noun" -allmatches
这篇关于使用 powershell 查找与两个单独的正则表达式匹配的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!