Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 3—RegExs for Converting to V2.0)

When and If the Time Comes, Regular Expressions (RegEx) Can Help with the Conversion Process from AutoHotkey V1.1 to V2.0

Identified by the (v1,v2) on the right side of the script name in the index, I’ve converted a few of the script on the Free AutoHotkey Script page from AutoHotkey V1.1 to the alpha version of V2.0. At first, I reworked a copy of a script one line at a time. Then I speeded up the process with a couple of Regular Expressions (RegEx) used in conjunction with Ryan’s RegEx Tester. While I continued working one line at a time, I could quickly reformat the entire line at once—mostly. Rather than tediously rewriting a command character by character, the RegEx provides a format which needs very little additional editing.

(I run AutoHotkey V1.1 and the yet-to-be-release V2.0 simultaneously using the techniques discussed in an earlier blog.)

Cover 200Note: I don’t explain how each RegEx in this blog works. For the uninitiated, Regular Expressions can get pretty confusing. (For that matter, even people very familiar with them can get flummoxed.) You should be able to use the code offered here without understanding how it works—although a little insight certainly helps. For more information on Regular Expressions (RegEx), see this “Introduction to Regular Expressions (RegEx) in AutoHotkey“, my blog “Deleting Double Words with AutoHotkey Regular Expressions RegEx)“, and/or the AutoHotkey Regular Expression (RegEx) Quick Reference page.

Converting to AutoHotkey V2.0 Functions Which Assign a Value

In the blog, “Comparing Today’s AutoHotkey Version 1.1 and the Future Version 2.0 (Part 1—Everything Functions)“, I demonstrated how the FormatTime command in V1.1 converts to V2.0 FormatTime() function.

AHK Format V2
The TimeString output variable in the V1.1 command becomes the new variable assignment for the V2.0 function.

The output variable TimeString moves from the first parameter in the V1.1 command to the new variable created in V2.2 using the := assignment operator. While you can use a cut-and-paste procedure to reformat the command, the appropriate RegEx makes the task much easier.

Using the RegExReplace tab in Ryan’s RegEx Tester, I entered the AutoHotkey v1.1 command into the Text to be searched box, added the Regular Expression (RegEx), inserted the Replacement Text, then copied the Results for use in the V2.0 script.

V2 RegEx Convert
After entering the AutoHotkey V1.1 command into the Text to be search box, I added the Regular Expression, then inserted the Replacement Text code, and copied the V2.0 function from the Results box.

This V1.1 command to V2.0 function conversion uses the following RegEx:

(.+?),(.+?),\s?(.+?),\s?(.+)

and the Replacement Text:

$2 := $1("$3","$4")

If you remember from the book, A Beginner’s Guide to Using Regular Expressions in AutoHotkey, each set of parentheses in the RegEx corresponds to a backreference number consecutively (e.g. $1, $2, $3, $4). By changing the order in the Replacement Text and adding the appropriate operator, parentheses, and quotes, the RegEx rearranges and reformats the captured backreference text.

This example does not represent a perfect RegEx for the job—merely a good start. First, the RegEx uses commas as delimiters, therefore, the V1.1 command must contain the optional comma just after the main command before returning a good result. If you don’t see the comma after the command, insert it.

Second, the Replacement Text assumes that it must enclose all function parameters with quotation marks. Most of the time, this works fine. However, when converting variables enclosed with percent signs (e.g. %A_Now%), V2.0 drops both the quotes and the percent signs, directly evaluating the variable. You need to remove any “% and %” found in your Results. (Next time, I’ll offer techniques for dealing with %var% replacements when converting from V1.1 to V2.0.)

You could write a script which uses the RegExReplace() function multiple times, but the variations in the different command formats would make writing the conversion’s RegExs a mammoth undertaking. (If I were to write a script conversion app, I would most likely create a lookup table for each V1.1 command which returns the appropriate RegEx for the command. Even then, the acceptable sloppiness of V1.1 syntax creates a significant challenge.) Plus, unlike with Ryan’s RegEx Tester, in the RegExReplace() function you must escape any V1.1 quotation marks with a second quote:

RegExReplace("FormatTime, TimeString, %A_Now%, MMMM d, yyyy"
      , "(.+?),(.+?),\s?(.+?),\s?(.+)" , "$2 := $1(""$3"",""$4"")")

(Word wrapped using AutoHotkey line continuation.)

A RegEx for GUI Control Commands

The GUI command in AutoHotkey V1.1 and the GUI function in V2.0 use the same parameters in the same order. This makes the conversion a little more straightforward—although still not ideal.

V2 RegEx Convert GUI

As with the first RegEx for the FormatTime() function, commas delimit each portion of the command. That means all four commas must appear in their proper location. In some forms of the GUI command, the script may omit the first comma (e.g. Gui Add as opposed to Gui, Add). Plus, in commands with no third parameter, you might find only two or three commas (e.g. Gui Add, Edit, w200 vLastName). AutoHotkey V1.1 doesn’t care whether you use those commas or not. However, for the RegEx conversion to work, you must insert the missing commas (and add at least one space after a trailing comma).

The abbreviated form of the Gui command (only two commas):

Gui Add, Edit, w200 vFirstName ym
Gui Add, Edit, w200 vLastName

must include all four commas for the RegEx conversion to work:

Gui, Add, Edit, w200 %x% %y% vFirstName ym,  ; plus a space at the end
Gui, Add, Edit, w200 vLastName,              ; plus a space at the end

One blank space must appear after a lone terminating comma. Including all four commas and the final space enables the Regular Expression:

(.+?),\s+?(.+?),\s?(.?|.+?),\s?(.?|.+?),\s?(.?|.+?)\n

using the following Replacement Text:

$1.$2("$3","$4","$5")
[Hidden RETURN must occur at end of code.]

to convert the above Gui commands to the following V2.0 functions:

Gui.Add("Edit","w200 %x% %y% vFirstName ym","")
Gui.Add("Edit","w200 vLastName","")

By adding the \n to the end of the RegEx, the RegEx Tester converts each line individually. Make sure you include a Return at the end of the last line of input code by pressing ENTER at the end of the Text to be searched. Plus, enter an invisible RETURN at the end of the Replacement Text code.

A major advantage comes with using Ryan’s RegEx Tester. The Results field instantly updates the changes as you add/delete the commas and terminating new lines in the Text to be searched field.

The final sticking point involves variables enclosed with percent signs (e.g. %x% %y%). Since the AutoHotkey V2.0 functions use expressions rather than the plain text found in V1.1 commands, those variables must appear without any quotation marks. The RegEx above does not resolve that problem. Next time, I’ll offer a couple of alternatives for converting parameters which include V1.1 %var% replacements to V2.0 expressions.

jack

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s