如何使用PowerShell的select-string从.htm文件中提取文件名?

时间:2010-01-21 16:27:43

标签: html regex powershell

我正在尝试搜索我们内联网的.htm文件,以找出哪些网络文件链接到该网站的哪些页面。我想要做的是让PowerShell遍历每个.htm并返回以“file:///”开头并以双引号结尾的任何字符串。例如:

<td colspan="3"><a href="file:///X:/Name of Document.doc" style="text-decoration: none">

会回来:

file:///X:/Name of Document.doc

至于PowerShell命令,我一直在使用它:

select-string -Path [Document Path] -Pattern '[Pattern]' -AllMatches | % { $_.Matches } | % { $_.Value }

我遇到的唯一麻烦是我无法弄清楚我应该使用的正则表达式来拉取我正在寻找的字符串。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

这种模式应该有效:`file:/// [^“] *'例如:

$str = @'
<td colspan="3">
    <a href="file:///X:/Name of Document.doc" style="text-decoration: none"> 
'@
$str | select-string '(file:///[^"]*)' | %{$_.Matches[0].Value}

file:///X:/Name of Document.doc