Reputation: 51109
I'm probably misunderstanding how PowerShell functions work, but I have the following to extract the year from a string such as "abcdef20201123xyz":
function getYear($str) {
$str -match ".*(?<year>[\d]{4})[\d]{4}.*"
return $matches['year']
}
I expected getYear("abcdef20201123xyz")
just to return "2020", but I get
True
2020
Why is this, and how do I just get what I put in the "return"?
Upvotes: 1
Views: 58
Reputation: 58981
In PowerShell, every not handled function call is sent to the output. So you either assign the return value to a variable, or you pipe it to Out-Null:
function getYear($str) {
$str -match ".*(?<year>[\d]{4})[\d]{4}.*" | out-null
return $matches['year']
}
Upvotes: 1