Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Test if string has any illegal characters for a variable name



  • Please log in to reply
9 replies to this topic
donkle
  • Members
  • 11 posts
  • Last active: Sep 04 2015 02:38 AM
  • Joined: 21 Sep 2013

Short version: *Without using error handling, what's the simplest way to check that a string can be safely used as a variable name.

 

Long version...

 

Because I'm forced to run some code on AHK Basic, I can't catch/handle errors, so I need some defensive code to prevent the "variable name contains an illegal character" before it happens.

 

Here's the AHK rules on a legal variable name...

 

"Variable names may be up to 253 characters long and may consist of letters, numbers and the following punctuation: # _ @ $ ? [ ]"

 
I know about the StrLen function.
 
But as for the list of legal characters, what's the simplest way to check that all characters in a string are variable-name legal?
 
Would regex be the way?  If so, would somebody mind posting the right regex to use?  I reckon it would take me an hour to cluestick myself into the right one.
 
thanks much.


Leef_me
  • Moderators
  • 8510 posts
  • Last active: Sep 10 2015 05:50 AM
  • Joined: 08 Apr 2009

I don't know regex, so I would use

 

if var contains +,.,^,&,!,=,/.~     ; <----- this list is incomplete

 msgbox illegal character



donkle
  • Members
  • 11 posts
  • Last active: Sep 04 2015 02:38 AM
  • Joined: 21 Sep 2013

I don't know regex, so I would use

 

if var contains +,.,^,&,!,=,/.~     ; <----- this list is incomplete

 msgbox illegal character

 

i didn't know about contains/in... so thanks.  I'm no longer stuck. 

 

i can Loop, Parse" the string and check that each char is found within a hardcoded list of the 69 legal chars..

 

26 a-z

26 A-Z

10 (numbers)

7 (punctuation)

 

but hopefully somebody will cook up a regex for it.  That's a nice clean, one-line solution.



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
varname := "not valid!"
if RegExMatch(varname, "^[\w#@$\?\[\]]{1,253}$")
    MsgBox valid name
else
    MsgBox invalid name
I think that covers everything, except for non-ASCII characters. To handle those as well:
if RegExMatch(varname, "^[\w#@$\?\[\]\x80-\xFF]{1,253}$")
For AutoHotkey 1.1, \?\[\] should be removed. For Unicode versions, \xFF should be replaced with \x{10ffff}.

donkle
  • Members
  • 11 posts
  • Last active: Sep 04 2015 02:38 AM
  • Joined: 21 Sep 2013

http://i.imgur.com/DWrI2JY.gif

 

BAM.  That's great.  I would've bolloxed it right up.  Thanks much.

 

 

For AutoHotkey 1.1, \?\[\] should be removed.

 

 

Huh?  Wouldn't that would remove the ? and [ and ] as legal characters... and aren't they legal?

 

http://www.autohotke...s/Variables.htm



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011
varname := "not valid!"
try 
{
	%varname% := %varname%
	MsgBox valid var name
}
catch
	MsgBox invalid var name
return


MindCaged
  • Members
  • 191 posts
  • Last active: Jul 22 2014 06:17 PM
  • Joined: 26 Aug 2012

http://i.imgur.com/DWrI2JY.gif

 

BAM.  That's great.  I would've bolloxed it right up.  Thanks much.

 

 

 

Huh?  Wouldn't that would remove the ? and [ and ] as legal characters... and aren't they legal?

 

http://www.autohotke...s/Variables.htm

Regarding ? [ and ], [ and ] are used in later versions for addressing array elements. Not sure about ? It might be the (expression)?(true result):(false result) syntax.

 

varname := "not valid!"
try 
{
	%varname% := %varname%
	MsgBox valid var name
}
catch
	MsgBox invalid var name
return

 

He specifically stated /without/ error handling since he's using AHK Basic.



Alpha Bravo
  • Members
  • 1687 posts
  • Last active: Nov 07 2015 03:06 PM
  • Joined: 01 Sep 2011
Oops, sorry should have known better than to offer support while jet lagged Zzzz

donkle
  • Members
  • 11 posts
  • Last active: Sep 04 2015 02:38 AM
  • Joined: 21 Sep 2013
✓  Best Answer

Regarding ? [ and ], [ and ] are used in later versions for addressing array elements. Not sure about ? It might be the (expression)?(true result):(false result) syntax.

Ah.  I see.  So I guess the AHK documentation could use some revision/organization/cleanup in places.  I reckon this is a sort of a known thing for AHK veterans.  

 

Since we all have such vast amounts of free time to offer volunteer projects, I'm surprised this hasn't been addressed yet. 

 

EDIT: Thx  for the help erbody.  (marked solved)



Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

So I guess the AHK documentation could use some revision/organization/cleanup in places.

The documentation you are using is for v1.0.48.05, because the site administrator is too negligent to update it and refuses to accept help. Up-to-date online documentation can be found here.