Reputation: 379
I'm new to Regex so don't judge hard, this is my email body:
INTERFACE ATM0/0/0 NOW MATCHES UD
Notification Type: INT-PHYSICAL
Host: Bla Bla
Address: xxx.xx.xxx.xx
State: UP
Date/Time: Thu Dec 11 21:07:05 GMT 2014
...
I'm using VBA macros to export emails into CSV file and then I import data to PowerShell and trying to match body to RedEx code below. I need to match only this line - INTERFACE ATM0/0/0 NOW MATCHES UD
and put result into a variable - AlertName, but the problem is that this line is not the same for every email. The parts of this line which always the same are INTERFACE[space]
and [space]NOW[space]MATCHES[space]
.
This is what I've got so far:
If($_ -match "(?<AlertName>[INTERFACE\s\w -/\sNOW\sMATCHES\s\w \r?\n]]+?)")
or
If($_ -match "(?<AlertName>[INTERFACE\s.*\r?\n]+?)")
But it doesn't work!
Could you please help me?
Thanks in advance!
Upvotes: 2
Views: 10412
Reputation: 32155
PowerShell stores matches from the -match
operator in the $matches[]
hash table.
You can also use the [Regex]
class from .NET directly (aka, [System.Text.RegularExpressions.Regex]
) if you need more features.
Upvotes: 1
Reputation: 22821
Not that clear what you're doing, but something along the lines of the following will help
if($_ -match "(INTERFACE.*NOW\s+MATCHES.*)") {
$alertName = $matches[1]
}
The special variable $matches
will contain information about the match, and $matches[1]
will contain the first group capture - i.e. what is matched between the brackets (...)
Upvotes: 6