在一个句子中找到多个关键词



我目前正试图在一个相当大的文本文档中搜索多个关键字,例如:我需要搜索区域和纽约是否出现在一个句子中。我目前的脚本只提供给我一个或另一个,而不是两个。

我当前的脚本是:

Get-Content <file name>.txt | Select-String '(phrase)'

任何想法吗?

如何获取多个模式的结果:

get-content "YourTextFile.txt" | select-string -pattern '(regional.*new york)|(new york.*regional)'

本质上,这将查找两个短语以任意顺序出现在同一行中的任何内容。

阅读这篇文章,这里的一些内容是我用来匹配模式的。通配符和。将是你的票。http://ss64.com/ps/syntax-regex.html

您可以使用Regex对象来执行这样的搜索。下面是它的样子:

$inFile = get-content c:temptest.txt
$matchedLines = New-Object -TypeName System.collections.ArrayList
foreach ($line in $inFile)
{
    $match = [regex]::Match($line, '(?i).*regional.*new york.*|(?i).*new york.*regional.*')
    if ($match.Success -eq $true)
    {
        $matchedLines.Add($match)
    }
}
Write-Output $matchedLines.Value

相关内容

最新更新