AutoHotkey Tip of the Week: Repeat Words and Phrases with RegEx Hotstrings

Save Time with This RegEx Hotstring for Inserting Repeated Words or Sentences—”Blah!” Instantly Turns Into  “Blah! Blah! Blah!”

regexrobotcartoonAt the end of my last blog, I postulated the possibility of a word duplicating RegEx Hotstring. While I don’t know how many people would ever use it, I do remember a time when the technique would have come in handy (as shown in the cartoon on the left). I thought that I would leave the problem as a reader’s challenge and move on, but I found that I couldn’t just abandon the loose end.

While this trick may not embody the most essential Hotstring, the technique might stimulate other AutoHotkey users to venture forward with their own variations on RegEx Hotstrings. I would love to hear about other innovative applications of the RegExHotstring() function—doing things that prove difficult (or impossible) with either traditional double-colon Hotstrings or the built-in Hotstring() function.

Repeating Words and Phrases

From my last blog:

I have an idea for a RegEx Hotstring which inserts a word multiple times by merely adding the number of instances at the end of the word:

Rah!3 ⇒ Rah! Rah! Rah!
go,4 ⇒ go, go, go, go,

I’m pretty sure that I would need to write a subroutine with a loop—although, I don’t know how much I would use this type of Hotstring trick?

I took the time to write a RegEx Hotstring function call and subroutine. The function identifies words or phases for repeated insertion:

RegExHotstrings("\b([[:alpha:]-'\s,]+)([!,.;?]?)(\d+)``" ,"Repeat")

The subroutine Repeat  inserts the captured text the designated number of times:

Repeat:
  OutputVar := ""
  Loop %$3%
    OutputVar := OutputVar . $1 . $2 . " "
  SendInput, {Raw}%OutputVar%
Return

To activate the text replication, type the target word(s) followed by the number iterations and the backtick mark (`).

Go Team!3` ⇒  Go Team! Go Team! Go Team!

The Regular Expressions (RegEx) introduces a couple of features for capturing words or phrases.

Note: Requires the RegExHotstring() function in the script, included (#Include), or in one of the function libraries.

How the RegEx Works

As discussed last time, the sets of parentheses register the critical parts of the expression. This RegEx includes three such sets (shown in red):

\b([[:alpha:]-'\s,]+)([!,.;?]?)(\d+)``

The contents of the parentheses return matched text as follows:

([[:alpha:]-'\s,]+) ⇒ $1 ⇒ Words or phases
([!,.;?]?) ⇒ $2 ⇒ Ending punctuation
(\d+) ⇒ $3 ⇒ Number of repetitions

The subroutine Repeat requires these three matches. Outside the enclosed portions of the expression, you see the leading boundary marker (\b) and a following double-backtick mark (``) which serve the following purposes:

  1. Cover 200
    Make Regular Expressions (RegEx) part of your AutoHotkey life with A Beginner’s Guide to Using Regular Expressions in AutoHotkey!

    The boundary marker (\b) ensures that the matched expression does not begin with a non-word character (space, punctuation, etc). Even though commas and spaces may appear inside the text, they may not act as the first character in the word or phrase. This forces the capture of the entire word or sentence.

  2. I selected the backtick (`) mark to initiate the word repeat feature because I almost never use it as part of a word. Plus, on my keyboard, it doesn’t require the use of the Shift key. However, since the backtick acts as the escape character in AutoHotkey, I needed to precede it with another backtick (``).

The Word or Phrase RegEx

The variable $1 captures the text matching the word or phrase targeted for repetition:

([[:alpha:]-'\s,]+)

This RegEx includes the following operators:

  1. This expression uses the POSIX set name [:alpha:]  matching any alphabetic character inside a range (i.e. [[:alpha:]]). Since the \w operator (which includes numeric digits) can cause confusion in the second part of the RegEx, I switched to [:alpha:] (the equivalent of adding A-Za-z to the range).
  2. The hyphen (-) and apostrophe (') inside the range allows the inclusion of hyphenated words and contractions.
  3. Adding the space (\s) and a comma (,) to the range allows for multiple words and/or intervening commas in a phrase or sentence.
  4. The plus sign (+) tells AutoHotkey that a lone word must include at least one (or more) alphabetic characters.
  5. Numbers cannot appear inside a word or phrase. I accepted this compromise since it could cause confusion for the iteration number expression when ending the word or phrase with a comma.

The Optional End Punctuation RegEx

The variable $2 saves the punctuation mark at the end of the word or phrase (if any):

([!,.;?]?)

This RegEx breaks down as follows:

  1. The range [!,.;?] accepts any of those five punctuation marks at the end of the final word.
  2. However, the final question mark [!,.;?]? renders any end character optional. This allows for words and phrases terminated with one of the five punctuation marks or no end character at all.

The RegEx continues capturing text until you enter the number of insertions.

The “How Many Insertions?” RegEx

The variable $3 stores the number of times to repeat the insertions:

(\d+)
  1. As seen in numerous earlier blogs, the \d operator matches any digit 0-9. Adding the plus sign (+) forces one or more digits in the captured numeric value.

For successful activation, you must immediately append one or more digits to the target word or phrase—no space. The Repeat subroutine uses the $3 numeric value to control the number of iterations for the replacement text building Loop command.

The Repeat Subroutine

The Repeat subroutine executes when you hit the backtick key (`) at the end of the RegEx. The subroutine iterates through a Loop concatenating the matched word or phrase to itself the designated number of times with an intervening space:

Repeat:
  OutputVar := ""
  Loop %$3%
    OutputVar := OutputVar . $1 . $2 . " "
  SendInput, {Raw}%OutputVar%
Return

The SendInput command uses the {raw} output option to prevent the exclamation point Hotkey modifier (!​) from causing unwanted Windows behavior.

One Possible Use for This Repeat Work RegEx Hotstring

As I told my 13-year-old grandson:

This week, I wrote a short Hotstring which inserts a word into any document as many times as you like after typing it just once. You could probably use it for your homework. For example, if you need to add another 100 words to a paper to make it to 500 words for your English class, you could just type “Blah!” once, enter the number of times you want to insert it “100” and hit the backtick key (`)—Blah!100`:

Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah! Blah!”

Count them. Think of the time it would save you when you can’t think of anything to write!

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.)

jack

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.)

Find my AutoHotkey books at ComputorEdge E-Books!

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