2016-04-07 25 views
-4

正則表達式來自想要提取特定事物的行。例如:在我有的行中:這是一個測試2016/01/23這是測試04:05 AM這是一個測試成功登錄在01.33秒。我如何只提取日期,時間&成功的時間。所以輸出將是用於PS腳本的正則表達式從行中讀取


(2016/01/23 04:05 AM成功01.33秒)。

感謝您的幫助。乾杯!

回答

0

我懷疑我會回答,然後你會發表評論「我的真實數據完全不同,我該如何匹配?」。讓我們來看看。

# Here is your log line 
$logLine = 'This is a test 2016/01/23 This is test 04:05 AM this is a test Success Login at 01.33 sec' 

# Here is my regex which matches 
#   * date   * time AMPM * Success Login  time  * 
$myRegex = '.*(\d{4}/\d\d/\d\d).*(\d\d:\d\d \S\S).*Success Login at ([0-9.]+ \S+).*' 

# Here is the test and output building 
if ($logLine -match $myRegex) { 
    Write-Host "($($matches[1]) $($matches[2]) Success $($matches[3]))" 
} else { 
    Write-Host "Log doesn't match" 
} 
+0

謝謝!有用。只有一件事。當我有下一行失敗時,它仍然顯示成功。我如何根據失敗/成功進行排序?我感謝你的意見。 –

+0

@SamTam你是什麼意思?下一行是什麼樣的?你想要排序什麼? – TessellatingHeckler