[英]Powershell Regex not triggering
I'm having a problem getting a Regex to extract a portion of a string and I can't see what I have done wrong. 我在让正则表达式提取字符串的一部分时遇到问题,但看不到我做错了什么。
The string: 字符串:
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
The code: 编码:
$destinationregex = "Destination: (.*)Start time:"
If ($message -match $destinationregex)
{
$destination = $matches[1]
}
I am trying to extract the text Network Location 我正在尝试提取文本“网络位置”
Any pointers would be appreciated! 任何指针将不胜感激!
As requested a fuller scope of code $events = get-eventlog application -source BackupAssist -newest 50 根据要求,代码的完整范围为$ events = get-eventlog应用程序-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
}
Okay, I'm posting as an answer this time, for more flexible formatting, and because I'm confident this will finally solve the problem. 好的,我这次发布的答案是,用于更灵活的格式设置,并且因为我有信心这最终可以解决问题。 ;)
;)
Try this: 尝试这个:
$destinationregex = '(?s)Destination: ([^\r\n]*).*?Start time:'
The (?s)
means that wildcards can match newline characters. (?s)
表示通配符可以匹配换行符。 The grouping ([^\\r\\n]*)
matches until the end of the line, and the .*?
分组
([^\\r\\n]*)
匹配到该行的末尾,并且.*?
matches the line break. 匹配换行符。 The following would work too:
以下内容也可以工作:
$destinationregex = 'Destination: (.*?)\\r\\nStart time:'
In fact, since you really only want to match from after "Destination: " to the end of the line, you could just do this, which is the simplest (unless you specifically want to make sure that you only match if "Start time:" is the very next thing): 实际上,由于您实际上只想从“ Destination:”之后到行尾进行匹配,因此您可以这样做,这是最简单的(除非您特别想确保仅在“ Start time: ”是接下来的内容):
$destinationregex = 'Destination: (.*)'
If it still doesn't work, it may be because $message is being read as an array instead of a string. 如果仍然不起作用,可能是因为$ message被读取为数组而不是字符串。 You can easily test that by adding the debugging line
$message.GetType()
immediately after setting $message. 您可以通过在设置$ message之后立即添加调试行
$message.GetType()
轻松地进行测试。 If it's an array, try setting $message this way (in addition to using one of the regular expressions above): 如果是数组,请尝试以这种方式设置$ message(除了使用上述正则表达式之一):
$message = $event.message | Out-String
In fact, doing it this way works in either case, but the | Out-String
实际上,无论哪种情况,都可以这样做,但是
| Out-String
| Out-String
is superfluous if it's already a string, though it doesn't hurt. 如果已经是字符串,则
| Out-String
是多余的,尽管它不会造成伤害。
For clarity, here's the modified code block, as I think it will end up, depending on the answers to the questions above: 为了清楚起见,这是修改后的代码块,我认为它将最终完成,具体取决于上述问题的答案:
foreach ($event in $events)
{
$message = $event.message | Out-String
$destinationregex = 'Destination: (.*)'
If ($message -match $destinationregex)
{
$destination = $matches[1]
}
else
{
$destination = "Unknown"
}
Write-Host $destination
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.