Created
December 22, 2020 11:13
-
-
Save gitfvb/6cbaf326743ffbf8d809b622156da5af to your computer and use it in GitHub Desktop.
Check some datatypes in PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Is-Numeric 10 # True | |
Is-Numeric "10" # True | |
Is-Numeric "10f" # False | |
Is-Numeric "+10" # True | |
Is-Numeric "-10" # True | |
Is-Numeric "-10.5" # False | |
#> | |
function Is-Int ($value) { | |
return $value -match "^[+-]?[\d]+$" | |
} | |
<# | |
Is-Float 10 # False | |
Is-Float "10" # False | |
Is-Float "10." # False | |
Is-Float "10.5" # True | |
Is-Float "10.545" # True | |
Is-Float "+10.5" # True | |
Is-Float "-10.5" # True | |
#> | |
function Is-Float () { | |
return $value -match "^[+-]?[\d]+[\.]+[\d]+$" | |
} | |
<# | |
# This should be True | |
Is-Link "https://www.apteco.com/blog?customerId=123" | |
# This should be False | |
Is-Link "ftp://www.google.com" | |
#> | |
function Is-Link ($value) { | |
$regexForLinks = "(http[s]?)(:\/\/)({{(.*?)}}|[^\s,])+" | |
$containedLinks = [Regex]::Matches($value, $regexForLinks) | Select -ExpandProperty Value | |
return $containedLinks.count -eq 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment