.net - Powershell Regex not triggering -


i'm having problem getting regex extract portion of string , can't see 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 trying extract text network location

any pointers appreciated!

as requested fuller scope of code $events = get-eventlog application -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 answer time, more flexible formatting, , because i'm confident solve problem. ;)

try this:

$destinationregex = '(?s)destination: ([^\r\n]*).*?start time:' 

the (?s) means wildcards can match newline characters. grouping ([^\r\n]*) matches until end of line, , .*? matches line break. following work too:

$destinationregex = 'destination: (.*?)\r\nstart time:'

in fact, since want match after "destination: " end of line, this, simplest (unless want make sure match if "start time:" next thing):

$destinationregex = 'destination: (.*)'

if still doesn't work, may because $message being read array instead of string. can test adding debugging line $message.gettype() after setting $message. if it's array, try setting $message way (in addition using 1 of regular expressions above):

$message = $event.message | out-string

in fact, doing way works in either case, | out-string superfluous if it's string, though doesn't hurt.

for clarity, here's modified code block, think end up, depending on answers 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 } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -