我有一個問題,得到一個正則表達式來提取字符串的一部分,我看不出我做錯了什麼。Powershell正則表達式不觸發
字符串:
Backup job DailyBackupToNAS completed successfully. Destination: Network location Start time: 01/05/2013 05:00:28 End time: 01/05/2013 05:39:13 Duration: 00:38:45.5875346
代碼:
$destinationregex = "Destination: (.*)Start time:"
If ($message -match $destinationregex)
{
$destination = $matches[1]
}
我想提取文本網絡位置
任何指針將不勝感激!
按照要求的代碼 $事件更充分的範圍=獲取,事件日誌應用-source BackupAssist -Newest 50
Foreach ($event in $events)
{
$message = $event.message
$destinationregex = "Destination: (.*)Start time:"
If ($message -match $destinationregex)
{
$destination = $matches[1]
}
Else
{
$destination = "Unknown"
}
Write.Host $destination
}
我用你的例子測試,併爲我工作。添加你的錯誤... – 2013-05-03 10:21:55
這不是錯誤,但目的地結束了一個空的變量。 – Trinitrotoluene 2013-05-03 10:22:46
檢查'$ message'的內容,如果是[string]類型。如果你使用你的例子來模擬,你會發現這個正則表達式是好的,我會在'(。*)'之後添加一個額外的空格來將它從捕獲中排除。 – 2013-05-03 10:26:35