regex - Powershell在word文档中搜索匹配字符串

标签 regex powershell search ms-word powershell-2.0

我有一个简单的要求。我需要在 Word 文档中搜索一个字符串,因此我需要在文档中获取匹配的行/一些单词。

到目前为止,我可以在包含 Word 文档的文件夹中成功搜索字符串,但它根据是否可以找到搜索字符串返回 True/False。

#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path     = "c:\MORLAB"
$files    = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
$output   = "c:\wordfiletry.txt"
$application = New-Object -comobject word.application
$application.visible = $False
$findtext = "CRHPCD01"

Function getStringMatch
{
  # Loop through all *.doc files in the $path directory
  Foreach ($file In $files)
  {
   $document = $application.documents.open($file.FullName,$false,$true)
   $range = $document.content
   $wordFound = $range.find.execute($findText)

   if($wordFound) 
    { 
     "$file.fullname has $wordfound" | Out-File $output -Append
    }

  }
$document.close()
$application.quit()
}

getStringMatch

最佳答案

#ERROR REPORTING ALL
Set-StrictMode -Version latest
$path     = "c:\Temp"
$files    = Get-Childitem $path -Include *.docx,*.doc -Recurse | Where-Object { !($_.psiscontainer) }
$output   = "c:\temp\wordfiletry.csv"
$application = New-Object -comobject word.application
$application.visible = $False
$findtext = "First"
$charactersAround = 30
$results = @{}

Function getStringMatch
{
    # Loop through all *.doc files in the $path directory
    Foreach ($file In $files)
    {
        $document = $application.documents.open($file.FullName,$false,$true)
        $range = $document.content

        If($range.Text -match ".{$($charactersAround)}$($findtext).{$($charactersAround)}"){
             $properties = @{
                File = $file.FullName
                Match = $findtext
                TextAround = $Matches[0] 
             }
             $results += New-Object -TypeName PsCustomObject -Property $properties
        }
    }

    If($results){
        $results | Export-Csv $output -NoTypeInformation
    }

    $document.close()
    $application.quit()
}

getStringMatch

import-csv $output

有几种方法可以得到你想要的东西。一个简单的方法是因为您已经拥有文档文本,可以对其执行正则表达式匹配并返回结果等等。这有助于尝试解决在文档中出现一些单词的问题。

我们有变量 $charactersAround它设置了在 $findtext 周围匹配的字符数.此外,我认为输出更适合 CSV 文件,所以我使用了 $results捕获属性的哈希表,最终将其输出到 csv 文件。

请务必更改用于您自己的测试的变量。现在我们正在使用正则表达式来定位匹配项,这开辟了一个充满可能性的世界。

样本输出
Match TextAround                                                        File                          
----- ----------                                                        ----                          
First dley Air Services Limited dba First Air meets or exceeds all term C:\Temp\20120315132117214.docx

关于regex - Powershell在word文档中搜索匹配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27169043/

相关文章:

javascript - 搜索表单重定向到结果页面

java - 正则表达式从字符串中获取特定值

regex - 正则表达式匹配相对和绝对 URL

javascript - 正则表达式提取WUB内的字符

python - 如何检查Python中的列表列表中是否存在某个元素?

java - 在 ArrayList 中搜索字符串中的特定字符

python - 如何在正则表达式中对 "or"匹配进行分组?

powershell - 使用 powershell 加载 ntuser.dat

powershell - 如何在Mercurial中打印特定文件的所有修订的内容?

linux - 从 Powershell 确定操作系统版本、Linux 和 Windows