I thought substring is easy until I tried:
# extract from a webpage with regex into $mmdd the value like 08/09
Write-Host $mmdd
08/09
$mmdd.Substring(0,2)
Then I get:
Method invocation failed because [System.Double] does not contain a method named 'Substring'. At line:1 char:1 + $test.Substring(0,2) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
> $test.Substring(3)
Method invocation failed because [System.Double] does not contain a method named 'Substring'. At line:1 char:1 + $test.Substring(3) + ~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound
How do I cast the variable $mmdd
to string so I can use Substring()
on it?
I suspect I have to do something to the following:
$page | Select-String -Pattern $mmddyyyyRgx -AllMatches |
% { $_.Matches } |
% { $_.Value } |
Select-String -Pattern $mmddRgx -AllMatches |
% { $_.Matches } |
% { $_.Value } -Outvariable mmdd
Since $mmdd gave me:
0.888888888888889
it's a double balue, casting like below will not work as expected:
[string]$mmdd.Substring(0,2)
it will give me 0. instead of 08