Regular Expressions (RegEx) Can Simplify a Tedious Code Reformatting Problem
Recently, I received the following comment from Thom:
Greetings,
A small improvement to the Autocorrect AHK script. I have been using this script for years and find it very useful. I was always a bit intrigued about the section of ambiguous entries which was commented out and not much use.
I was fascinated to read about your TextMenu function [found in the book Beginning AutoHotkey Hotstrings] to display the various choices. I found a simple way with RegEx to change all the entries in the section.
For example:
::electon::election, electron
To:
::electon:: TextMenu("election, electron") Return
I copied and pasted the list into Notepad++ and then ran this find-and-replace.
Find:
(::\w+::)(.+)
Replace:
$1\n TextMenu\(\"$2\"\)\nReturn
And presto it works—some entries need tweaking but it works well.
* * *
Thanks for the example, Thom! I’ve played around with the TextMenu() function for updating that portion of AutoCorrect myself—although I used Ryan’s RegEx Tester to do the work.
I have one recommendation. Since I wrote my first TextMenu() function, AutoHotkey introduced the X option for Hotstrings which runs AutoHotkey commands and functions directly. To save space, I would use the following code in the RegEx Tester:
Note the differences between Thom’s code and that in the replacement text field of the Tester. Ryan’s Tester simulates the values used in the RegExReplace() function and doesn’t require escaping (e.g. \( for an open parenthesis or \” for a double-quote, respectively). With the exception of the saved subpatterns ($1 and $2), the AutoHotkey function uses mostly plain text in the replacement field—although I did terminate it with the RETURN key.
For a quick look at the meaning of the RegEx code used above, see the AutoHotkey Regular Expressions Reference or check out this list of RegEx blogs I posted in the “Tutorials” section of the AutoHotkey forum.
Regular Expressions (RegEx) for Reformatting Code
When making numerous repetitive changes to AutoHotkey code, Regular Expressions offer powerful tools. The recently added AutoHotkey Switch/Case command has prompted me to clean up much of my older, deprecated code. Often the sheer number of code lines makes RegEx the best possible solution for rewriting the scripts.
In the QuickLinks.ahk script, I originally used the IfInString command to add icons to the menus. Now deprecated, it behooves me to update to the new Switch/Case command:

Quite frankly, I should have been using a series of If-Else statements in the first place. Using the now deprecated IfInString command introduced a number of issues requiring special consideration. By updating to the Switch/Case command, I implement a simplified If-Else format with all of its benefits.
Learn How to Use Regular Expressions (RegEx)
When looking at making major repetitive changes in your code, consider using Regular Expressions (RegEx) whether through search-and-replace in a program such as Notepad++—as Thom did—or by using Ryan’s RegEx Tester—as I did. Admittedly, it takes a little time to learn how RegEx works. (That’s why I published the book A Beginner’s Guide to Using Regular Expressions (RegEx) in AutoHotkey.) But, in the long run, you’ll find it well worth the investment of time.
* * *
The book, A Beginner’s Guide to Using Regular Expressions in AutoHotkey, covers a number of Regular Expressions (RegEx) topics introducing practical techniques for making your AutoHotkey scripts more powerful. The first chapters introduce the concept and implementation of RegEx while the follow-on chapters demonstrate useful applications.
- Chapter Five discusses eliminating double words.
- Chapter Six discusses fixing contractions with RegEx.
- Chapter Seven shows how to swap letters or words.
- Chapter Eight uses RegEx to extract world location information about an IP address from a Web page.
- Chapter Nine shows you how to remove HTML tags from any document or source code.
- Chapter Ten demonstrates how to extract Web links from Web pages.
- Chapter Eleven offers a RegEx for verifying valid e-mail addresses with AutoHotkey.
* * *
In the course of working on the menu icon setting routines, I experienced a few more AutoHotkey revelations. I plan to address those insights in upcoming discussions of the QuickLinks.ahk script. For the curious, I recommend using QuickLinksTimeDateSubMenuSwitch.ahk as the latest version. The icon setting portions of the script now uses the newer Switch/Case command. (The Switch/Case command requires AutoHotkey version 1.1 November 2019 or greater to run.)
Click the Follow button at the top of the sidebar on the right of this page for e-mail notification of new blogs. (If you’re reading this on a tablet or your phone, then you must scroll all the way to the end of the blog—pass any comments—to find the Follow button.)
This post was proofread by Grammarly
(Any other mistakes are all mine.)
(Full disclosure: If you sign up for a free Grammarly account, I get 20¢. I use the spelling/grammar checking service all the time, but, then again, I write a lot more than most people. I recommend Grammarly because it works and it’s free.)