Page 1 of 1
Only a number (RegExMatch?)
Posted: 09 May 2024, 08:36
by Albireo
I want to check that a variable contains only numbers (0-9)
Or rather not be empty or contain anything other than numbers.
Code: Select all
#Requires Autohotkey 2.0
#SingleInstance Force
x := '123' ; Correct
; x := 'A12' ; Wrong info
; x := '' ; Wrong info
If !RegExMatch( x, "^[0-9]*" )
{ MsgBox "Error"
ExitApp
}
MsgBox x "`t(Only number)"
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 08:52
by mikeyww
Code: Select all
#Requires AutoHotkey v2.0
x := '123' ; Correct
digitsOnly := '^\d+$'
x := 'A12' ; Wrong info
x := '' ; Wrong info
MsgBox RegExMatch(x, digitsOnly)
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 08:54
by Seven0528
Code: Select all
isStringInteger(value) => !!(value ~= "D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$")
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
persistent
dec := "10"
hex := "0xA"
msgbox isInteger(dec) ; true
msgbox isInteger(hex) ; true
msgbox isInteger(dec A_Space) ; true
msgbox isInteger(dec A_Tab) ; true
msgbox (dec ~= "^\d+$") ; 1
msgbox (dec ~= "^[[:digit:]]+$") ; 1
msgbox (dec "`n" ~= "^\d+$") ; 1
msgbox (dec "`n" ~= "^[[:digit:]]+$") ; 1
msgbox (hex ~= "^0[Xx][0-9A-Fa-f]+$") ; 1
msgbox (hex ~= "^0[Xx][[:xdigit:]]+$") ; 1
msgbox (hex "`n" ~= "^0[Xx][0-9A-Fa-f]+$") ; 1
msgbox (hex "`n" ~= "^0[Xx][[:xdigit:]]+$") ; 1
msgbox (dec ~= "D)^(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 1
msgbox (hex ~= "D)^(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 1
msgbox (dec "`n" ~= "D)^(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 0
msgbox (hex "`n" ~= "D)^(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 0
dec := "-10"
hex := "-0xA"
msgbox (dec ~= "D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 1
msgbox (hex ~= "D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$") ; 1
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 10:27
by Albireo
Thanks for both ideas. (for me isn't hex a number)
The solution is similar to this .:
Code: Select all
#Requires Autohotkey 2.0
#SingleInstance Force
x := '123' ; Correct
; x := 'A12a' ; Wrong info
; x := '12&' ; Wrong info
; x := '1,2' ; Wrong info
; x := '' ; Wrong info
If !RegExMatch( x, '^\d+$' )
{ MsgBox x "`t(Error)"
ExitApp
}
MsgBox x "`t(Only number)"
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 10:32
by Datapoint
There's also a built in function:
https://www.autohotkey.com/docs/v2/lib/Is.htm#digit
MsgBox IsDigit('123')
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 10:53
by mikeyww
True if Value is a positive integer, an empty string, or a string which contains only the characters 0 through 9. Other characters such as the following are not allowed: spaces, tabs, plus signs, minus signs, decimal points, hexadecimal digits, and the 0x prefix.
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 10:57
by boiler
You can use
IsXDigit() to include hex numbers. Would have to check separately for empty strings, as pointed out.
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 11:36
by Albireo
IsDigit is more sensitive to what the contents of the variable are
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
; x := '123' ; Correct
; x := 'A12' ; Wrong info
; x := '12&' ; Wrong info
; x := '1,2' ; Wrong info
; x := '' ; Wrong info
x := 0 ; Correct
; x := "0" ; Correct
if !isInteger(x)
{ MsgBox x "`t (is NOT an Integer)"
}
if !isDigit(x)
{ MsgBox x "`t (is NOT a Digit)"
}
If !RegExMatch( x, '^\d+$' )
{ MsgBox x "`t (Not an Number)"
}
In the example above, the following error is generated
"IsDigit requires a String, but received an Integer."
A user rarely sees a difference on
var := 10 or
var := "10"
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 12:21
by Seven0528
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
intDec := 10
strDec := "10"
intHex := 0xA ; same as 10
strHex := "0xA"
; If you want to check if an integer or a string is in decimal.
msgbox "isStringDecimal()"
. "`n" isStringDecimal(intDec) ; true
. "`n" isStringDecimal(strDec) ; true
. "`n" isStringDecimal(intHex) ; true
. "`n" isStringDecimal(strHex) ; false
; If you want to check if an integer or a string is in hexadecimal.
msgbox "isStringHexadecimal()"
. "`n" isStringHexadecimal(intDec) ; false
. "`n" isStringHexadecimal(strDec) ; false
. "`n" isStringHexadecimal(intHex) ; false
. "`n" isStringHexadecimal(strHex) ; true
; If you want to check if it's an integer.
msgbox "isStringInteger()"
. "`n" isStringInteger(intDec) ; true
. "`n" isStringInteger(strDec) ; true
. "`n" isStringInteger(intHex) ; true
. "`n" isStringInteger(strHex) ; true
isStringDecimal(value) => !!(value ~= "D)^-?[[:digit:]]+$")
isStringHexadecimal(value) => !!(value ~= "D)^-?0[Xx][[:xdigit:]]+$")
isStringInteger(value) => !!(value ~= "D)^-?(?:[[:digit:]]+|0[Xx][[:xdigit:]]+)$")
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
intDec := 10
strDec := "10"
try msgbox isDigit(intDec) ; Error: Parameter #1 of IsDigit requires a String, but received an Integer.
msgbox isDigit(strDec) ; true
msgbox isDigit(intDec "") ; true
msgbox isDigit(strDec "") ; true
msgbox isDigit(string(intDec)) ; true
msgbox isDigit(string(strDec)) ; true
Code: Select all
#Requires AutoHotkey v2.0
#SingleInstance Force
strPosDec := "10"
strNegDec := "-10"
isStringDecimal(value) => !!(value ~= "D)^-?[[:digit:]]+$")
msgbox isDigit(strPosDec) ; true
. "`n" isDigit(strNegDec) ; false
msgbox isStringDecimal(strPosDec) ; true
. "`n" isStringDecimal(strNegDec) ; true
Re: Only a number (RegExMatch?)
Posted: 09 May 2024, 13:47
by Datapoint
Convert to string in case it is a pure number: IsDigit(String(x))
You would also need to check if it's blank, as has been pointed out.