我有一个问题,得到一个正则表达式来提取字符串的一部分,我看不出我做错了什么。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