Page 1 of 1

How to check if it contains characters other than letters, numbers and some ASCII special characters?

Posted: 12 Feb 2020, 11:51
by afe
How to check if it contains characters other than letters, numbers and some ASCII special characters?

a-z A-Z
0-9
space!#$%&'()\+,-.:;=@[\]^_`{}

Such as checking if it contains :
/ * ? " < > |
Non-ASCII characters

Code: Select all

str := "a1*"

MsgBox % InStr(str, "[^ !#\$%&'\(\)\+,-\.:;=@\[\\\]\^_`\{}~0-9a-zA-Z]")
What went wrong with this regular expression?

Re: How to check if it contains characters other than letters, numbers and some ASCII special characters?

Posted: 12 Feb 2020, 13:11
by boiler
What went wrong with your regular expression is you used InStr instead of RegExMatch.

Code: Select all

str := "a1*"

MsgBox % RegExMatch(str, "[^ !#\$%&'\(\)\+,-\.:;=@\[\\\]\^_`\{}~0-9a-zA-Z]")

Re: How to check if it contains characters other than letters, numbers and some ASCII special characters?

Posted: 13 Feb 2020, 04:30
by afe
Oh, I made a stupid mistake.
However, I found that when the regular expression is written as follows, it will not match "*". Should escape sequences be followed in regular expressions as well?

Code: Select all

str := "a1*"

MsgBox % RegExMatch(str, "[^ !#\$%&'\(\)\+,-\.0-9:;=@A-Z\[\\\]\^_`a-z\{}~]")

Escape Sequences
https://www.autohotkey.com/docs/commands/_EscapeChar.htm

Re: How to check if it contains characters other than letters, numbers and some ASCII special characters?  Topic is solved

Posted: 13 Feb 2020, 05:03
by boiler
Yes, the regular expression functions see the backtick character as an escape character. If you want to include it as a character of its own, use two in a row.

Re: How to check if it contains characters other than letters, numbers and some ASCII special characters?

Posted: 19 Feb 2020, 10:54
by afe
Thank you. You're right.

Code: Select all

MsgBox % RegExMatch(str, "[^ !#\$%&'\(\)\+,-\.0-9:;=@A-Z\[\\\]\^_``a-z\{}~]")