Only a number (RegExMatch?)

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albireo
Posts: 1774
Joined: 16 Oct 2013, 13:53

Only a number (RegExMatch?)

Post by Albireo » 09 May 2024, 08:36

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)"
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Only a number (RegExMatch?)

Post by mikeyww » 09 May 2024, 08:52

Code: Select all

#Requires AutoHotkey v2.0
x := '123'	; Correct
digitsOnly := '^\d+$'
x := 'A12'	; Wrong info
x := ''	; Wrong info
MsgBox RegExMatch(x, digitsOnly)
User avatar
Seven0528
Posts: 394
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Only a number (RegExMatch?)

Post by Seven0528 » 09 May 2024, 08:54

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
Last edited by Seven0528 on 09 May 2024, 12:19, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
Albireo
Posts: 1774
Joined: 16 Oct 2013, 13:53

Re: Only a number (RegExMatch?)

Post by Albireo » 09 May 2024, 10:27

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)"
User avatar
Datapoint
Posts: 302
Joined: 18 Mar 2018, 17:06

Re: Only a number (RegExMatch?)

Post by Datapoint » 09 May 2024, 10:32

There's also a built in function: https://www.autohotkey.com/docs/v2/lib/Is.htm#digit

MsgBox IsDigit('123')
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

Re: Only a number (RegExMatch?)

Post by mikeyww » 09 May 2024, 10:53

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.
User avatar
boiler
Posts: 17206
Joined: 21 Dec 2014, 02:44

Re: Only a number (RegExMatch?)

Post by boiler » 09 May 2024, 10:57

You can use IsXDigit() to include hex numbers. Would have to check separately for empty strings, as pointed out.
Albireo
Posts: 1774
Joined: 16 Oct 2013, 13:53

Re: Only a number (RegExMatch?)

Post by Albireo » 09 May 2024, 11:36

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"
User avatar
Seven0528
Posts: 394
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Only a number (RegExMatch?)

Post by Seven0528 » 09 May 2024, 12:21

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
Last edited by Seven0528 on 09 May 2024, 14:30, edited 1 time in total.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
User avatar
Datapoint
Posts: 302
Joined: 18 Mar 2018, 17:06

Re: Only a number (RegExMatch?)

Post by Datapoint » 09 May 2024, 13:47

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.
Post Reply

Return to “Ask for Help (v2)”