获取searchstring以及在searchstring

时间:2015-08-13 13:35:15

标签: regex powershell

$PartialResult = Foreach ($String in $MyStrings)
{Select-String -Path 'C:\MyWebsite.html' -list "(.*?)$String(.*?)|'\d\d\.\d\d\.\d\d\d\d'" -Context 2,1} 

向我展示了我的搜索字符串和日期,上面两行。 这很好用!

但有时日期是3,或者7或更多行。 如何获取我的搜索字符串以及在搜索字符串上方找到的第一个日期?

1 个答案:

答案 0 :(得分:0)

未经测试,但我认为这应该有效:

$PartialResult = Foreach ($String in $MyStrings)
{
  $regex = '(?ms)(\d\d\.\d\d\.\d\d\d\d).+?({0})' -f [regex]::Escape($String)
  if ( (Get-Content 'C:\MyWebsite.html' -Raw) -match $regex )
    { $Matches[1,2] }
 }