Skip to content

Instantly share code, notes, and snippets.

@gitfvb
Created December 22, 2020 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitfvb/6cbaf326743ffbf8d809b622156da5af to your computer and use it in GitHub Desktop.
Save gitfvb/6cbaf326743ffbf8d809b622156da5af to your computer and use it in GitHub Desktop.
Check some datatypes in PowerShell
<#
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