One of the Keyboard Maestro’s feature that I didn’t mention in building macro tips is the ability to search and replace clipboards with regular expression. What is a regular expression, and how does it help you to manipulate clipboards?

The typical pattern to manipulate clipboards is the search and replace feature that lets you replace the clipboards without using a text editor. But there is a limit of how much you can manipulate clipboards by searching and replacing an exact match word. This is where regular expression can solve the common obstacle we have in manipulating string: matching complex patterns.

Understanding Regular Expression

A regular expression is a text-matching technology. It’s the same as the Find & Replace feature you use in TextEdit. The only difference with a regular expression is you can have more options to match different kinds of text patterns.

We’ll be using RegExr to test the regular expression in the example thorough this guide (feel free to experiment with the regular expression as you read each section). You may use the cheatsheet and examples on RegExr to help you understand regular expression syntax.

This guide addresses the fundamental of a regular expression. Once you finish this, you should be able to write a decent regular expression to manipulate clipboards, extract the matched string, and assign it as a variable in Keyboard Maestro.

Pipe (|) for the alternation

Regular Expression: Alternation

When you need to match one or more patterns, you can use a pipe for alternation. You can have more than one pipe under one regular expression. If you want to match multiple email addresses, you can use [email protected]|[email protected]|[email protected] as an expression.

Parenthesis ( or ) for grouping

Regular Expression: Grouping

What if you want to match “My favorite fruit is banana” or “My favorite fruit is apple”? You can write the regular expression as My favorite fruit is apple|My favorite fruit is banana to match the sentence above, but you can also use parenthesis to group these two words together. The rewritten regular expression is My favorite fruit is (apple|banana).

Bracket ([ or ]) for character sets

Regular Expression: Sets

Character set matches any character inside the scope of brackets. Using [fun] as regular expression matches any text that has f, u, or n. The character set is useful when you need to match a specific pattern that carries a range of characters, such as numbers or lowercase letters. If you want to match id9829747, you can simply write the regular expression as id[0123456789] or use the simplified version as id[0-9]. The use of the dash in the bracket is known as character range. You can also use [a-zA-Z] to match the lowercase and uppercase alphabet.

Period (.) to match any character

Regular Expression: Period

Sometimes you want to match the words with a slightly different variations. This regular expression l..k matches text that has any two characters between l and k such as look, leek, and link.

People often mistype my name Sutanto as Susanto. I can use this regular expression su.anto to match any incoming emails that contain Sutanto, Sumanto, Susanto, with a single expression instead of using alternation.

Asterisk (*) and Plus Sign (+) for repetition

Asterisk and plus sign are quantifiers. They determine the number of times the precedented pattern should repeat itself. You‘ll find yourself using it along with a period to match long patterns such as URL, sentence, or paragraph.

An asterisk matches the precedented character, character set, or group zero time or more. The regular expression run*ing marks the precedented character n as optional, but still matches the text that carries the character more than once, such as ruining or runnnnning.

Plus sign matches the precedented character, character set, or group once or more. Replacing the asterisk in the previous regular expression into run+ing marks the precedented character n as necessary. The new regular expression will only match runing or running, but not ruining.

Regular Expression: Repetition

Keyboard Maestro has a feature to search clipboard by a regular expression and assign it as a variable. Combining quantifiers allows you to extract part of the text that you can use as a variable or combine it into a different clipboard. Let’s say I want to obtain the name of the application from:

https://itunes.apple.com/us/app/dash-docs-snippets/id458034879

I can write a regular expression /app/.+/ to get /app/dash-docs-snippets/. I can clean up the extracted clipboard by replacing the initial /app/ and ending / with an empty character. The end result will be dash-docs-snippets, which I can append as a campaign term for the tracking purpose.

Caret (^) to match the beginning of line

Regular Expression: Caret

Caret is useful when you want to replace only the beginning part of each line item and leave the rest of the text intact. I used to copy the description from Excel spreadsheet, which contains a lot of description with duplicate apostrophes at the beginning and end of the line like this one:

”””"”Somewhere mine is not working”, she said.”””””

I want to clean up the duplicate apostrophe. Using caret to mark the beginning of the line, I write this regular expression ^""+ to match the description that has more than two apostrophes at the beginning of the line. Once detected, I just need to replace it with single apostrophe which turns the clipboard into:

“Somewhere mine is not working”, she said.”””””

Dollar Sign ($) to match end of line

Regular Expression: Dollar Sign

Following the previous example, the end of the sentence also has duplicate apostrophes. I can use a dollar sign to match only the end of the sentence using this expression ""+$.

You can combine these two expressions into one with alternation, which is ^""+|""+$ and replace the detected text with a single apostrophe. Run the macro, and I‘ll have a cleaned up clipboard as following:

“Somewhere mine is not working”, she said.”

Backslash (\) to escape special characters

Regular Expression: Backslash

The most common regular expression method to escape the special character that I’ve listed here is by prepending it with a backslash (\). For example, when you need a regular expression to match period, you can enter the expression as \.. This is useful when you need to match URLs or sentences that contain a period.

Here are some typical special characters you’ll often use in matching text.

  • \n matches new line
  • \t matches tab
  • \r matches carriage return

Conclusion

This guide merely scratches the surface of a regular expression that deals with clipboard manipulation. The point for me to write this guide is to help you get started using Search Clipboard with Regular Expression action in Keyboard Maestro. With regular expression, you have more choices when dealing with clipboards. I hope you can create some awesome macros with this new knowledge.