Once you get used to the RegEx symbols it is pretty easy to follow however, those not familiar with it, will often be perplexed by what it is doing. I will sometimes use POSIX in AutoHotkey Regular Expressions to help layman read my code. While it is easier to read, it does add considerable length to the expression (but don’t confuse length with the speed of it working. I doubt there is a correlation in this case)
POSIX (pronounced PAH-zihcks) is an acronym for Portable Operating System Interface from UNIX
Using Regular Expressions in AutoHotkey is awesome! On more complex expressions, I like to use Named subpatterns in AutoHotkey Regular Expressions to, automatically, store the results in named variables which indicate what they represent.
I typically get my RegEx working properly then I go back and change the pattern to using a named subpattern. For instance if the following was your RegEx that you got working:
(\w\w\w)/(\d\d)/(\d\d\d\d) I would go back and adapt it to:
Video demonstrating how to create Named subpatterns in AutoHotkey Regular Expressions
Using RegEx to identify text patterns is an awesome skill-set to have. In the below tutorial we review some of the shortcuts /escapes in AutoHotkey Regular Expressions. These can make your code much more reader-friendly and easier to maintain.
Here’s a cheat-sheet for some of the more popular escapes:
- \w =word character
- \d = digit
- \s =white space (tab, space, etc)
- \b = word boundary
Remember, for each of the above, you can select the inverse of them simply by capitalizing the letter.
Demo video showing how to use Shortcuts / Escapes in AutoHotkey Regular Expressions
Using the AutoHotkey StringSplit / StrSplit() function
AutoHotkey has some pretty cool functions for slicing strings. Not quite as robust as Python however they definitely cover the majority of needs with ease. In the below video I demonstrate some simple usage if it as well as a way to access the Array it creates without ever saving it to a variable! 🙂
str:="my example string"
s:=StrSplit(str," ") ;Create an Array in "s" parsing on spaces
myVar:= s.1 ;Access the first item in the Array
MsgBox % myVar
Loop, $ s.MaxIndex() ;since it is an array you can get the count of items
MsgBox % s[A_index] ;iterate over the items in the array
Tutorial demonstrating AutoHotkey StringSplit function