Check Variable Contain Numbers/Letters Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
snowmind
Posts: 124
Joined: 12 Nov 2015, 15:18

Check Variable Contain Numbers/Letters

15 Aug 2019, 22:40

Hi People

Can anyone give me a hint?I need to validate if a variable contains letters and numbers together. I tried something else didn't work :|

Code: Select all

Var=UDP4324

if (RegExMatch(Var, "i)[^A-Z ^0-9 ]"))
{
	MsgBox, OK
}
else
{
	MsgBox, NO
}
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Check Variable Contain Numbers/Letters  Topic is solved

15 Aug 2019, 22:45

Cheers.

Code: Select all

;q:: ;check that string contains at least one number and at least one letter
Var=UDP4324
;Var=aaaaaaa
;Var=1111111
if RegExMatch(Var, "i)(?=.*[A-Z])(?=.*\d)")
;if RegExMatch(Var, "\d") && RegExMatch(Var, "[A-Za-z]") ;equivalent to line above
{
	MsgBox, OK
}
else
{
	MsgBox, NO
}
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
snowmind
Posts: 124
Joined: 12 Nov 2015, 15:18

Re: Check Variable Contain Numbers/Letters

16 Aug 2019, 07:33

jeeswg wrote:
15 Aug 2019, 22:45
Cheers.

Code: Select all

;q:: ;check that string contains at least one number and at least one letter
Var=UDP4324
;Var=aaaaaaa
;Var=1111111
if RegExMatch(Var, "i)(?=.*[A-Z])(?=.*\d)")
;if RegExMatch(Var, "\d") && RegExMatch(Var, "[A-Za-z]") ;equivalent to line above
{
	MsgBox, OK
}
else
{
	MsgBox, NO
}
return
really realy really thanksss :bravo:
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Check Variable Contain Numbers/Letters

16 Aug 2019, 07:53

Another approach: RegExMatch(var, "i)\d[a-z]|[a-z]\d")
User avatar
ThumpieBunnyEve
Posts: 109
Joined: 03 Aug 2016, 19:03

Re: Check Variable Contain Numbers/Letters

19 Aug 2019, 21:29

@teadrinker If var was "1,A+2.B" then your

Code: Select all

RegExMatch(var, "i)\d[a-z]|[a-z]\d")
version would report that there was not AlphaAndNumeric items in var.
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Check Variable Contain Numbers/Letters

19 Aug 2019, 21:56

I think the variable is supposed to contain only letters and numbers. If not, @jeeswg's code also can not always work:

Code: Select all

var := "a`r`n1"
MsgBox, % RegExMatch(Var, "i)(?=.*[A-Z])(?=.*\d)")
s option must be added.
User avatar
ThumpieBunnyEve
Posts: 109
Joined: 03 Aug 2016, 19:03

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 00:18

so all one has to do is add s to cover the possibility of carriage-returns & line-breaks.

Would this function name be too long? i forget if there is a limit on function name length.

Code: Select all

StrHasLettersAndDigits(Var){
return RegExMatch(Var, "is)(?=.*[A-Z])(?=.*\d)")
}

This reports 1 if the string has at least 1 letter and 1 number. Even if the string searched has new lines in it "`r`n".
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 06:30

ThumpieBunnyEve wrote: Would this function name be too long?
I don’t know anything about the permissible length of function names, but you can experiment:

Code: Select all

Loop 150
   str .= "a"

Func(str).Call()

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa() {
   MsgBox, % StrLen(A_ThisFunc)
}
Odlanir
Posts: 659
Joined: 20 Oct 2016, 08:20

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 06:55

teadrinker wrote:
20 Aug 2019, 06:30
I don’t know anything about the permissible length of function names, but you can experiment:
After some experimenting seems 253. I think the total is 255 so 253 + the 2 parenthesis is equal to 255.
____________________________________________________________________________
Windows 10 Pro 64 bit - Autohotkey v1.1.30.01 64-bit Unicode
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 07:36

Yeah, my test:

Code: Select all

SetBatchLines, -1
Loop {
   funcName .= "a"
   ToolTip % i := A_Index
} until !ExecScript("#NoTrayIcon`n" . funcName . "()`n" . funcName . "() {`nFileAppend, t, *`n}")
MsgBox, % "Max function name length: " . --i

ExecScript(script)  {
   shell := ComObjCreate("WScript.Shell")
   exec := shell.Exec(A_AhkPath . " *")
   exec.StdIn.Write(script)
   exec.StdIn.Close()
   return exec.StdOut.ReadAll()
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 07:50

Code: Select all

// -2 for the beginning and ending g_DerefChars:
#define MAX_VAR_NAME_LENGTH (UCHAR_MAX - 2)
in the variable case it says "line doesnt contain a recognized action". maybe it could be changed to refer to the length instead, same as in the function case?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Check Variable Contain Numbers/Letters

20 Aug 2019, 08:19

i cant, no cpp knowledge :cry:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], macromint, Spawnova and 353 guests