Target String:
8AM - 10AM (local time)
12PM - 1PM (local time)
I am able to extract the times from https://regexr.com/ with this expression:
\d{1,2}(AM|PM)
However,i am not able to do so in powershell:
cls
$var = "8AM - 10AM (local time)
12PM - 1PM (local time)"
if ($var -match "\d{1,2}(AM|PM)")
{
$matches
}
PS output:
Name Value
---- -----
1 AM
0 8AM
However, when I use the script below in PS:
cls
$var = "8AM - 10AM (local time)"
[regex]::Matches($var,"\d{1,2}(AM|PM)").Value
It gives the correct output:
8AM
10AM
Any ideas as to why this is happening?
The -match operator only ever looks for at most 1 match.
[regex]::Matches() is indeed the right choice if you need multiple matches.Select-String cmdlet, which will be much slower, however:('8AM - 10AM (local time)' | Select-String -AllMatches '\d{1,2}(AM|PM)').Matches.ValueThus, with -match, the automatic $Matches results variable only ever contains information about that 1 match, possibly including capture-group matches.
(AM|PM) part of your regex is a capture group, so for the 1st overall match - 8AM - that capture group's result is AM.$Matches is a [hashtable] whose entry with the 0 key contains the overall match; entry <n> contains the nth (unnamed) capture-group result.[hashtable] happens in no guaranteed order, the 1 entry happens to be listed before the 0 entry in this case.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With