Reputation: 521
I am using powershell with regex to try and extract the following time from the line below "01:42:35". However I want to ignore the time "02:42:35" but I am unsure of how to do it.
2013-07-04 02:42:35 Alert 172.172.19.9 Jul 4 01:42:35 ...
Currently I am using this time regex: $time_regex = "(\d+):(\d+):(\d+)"
How can I adapt this to the above specification?
Note: the time i am trying to get is not at the end of the line and the second time always has a date next to it in the format "Jul 4 " whereas the first time has a date next to it in the format "2013-07-04"
Thanks
Upvotes: 2
Views: 1393
Reputation: 60908
If is always at the end of the line use:
$t = "2013-07-04 02:42:35 Alert 172.172.19.9 Jul 4 01:42:35"
[regex]::match( $t, "(\d+:){2}(\d+)$" ) | select -expa value
Edit after comment:
try this:
$time_regex = "(?<= \d+ )(\d+:){2}\d+"
Upvotes: 1
Reputation: 336138
$time_regex = "(?<=\w+ \d+ )(\d+):(\d+):(\d+)"
will only match a time string that's preceded by an alphanumeric "word" and a number.
Upvotes: 1