User Tools

Site Tools


Regular_Expressions

Regular Expressions (RegEx)

Keyboard Maestro uses ICU Regular Expressions (aka RegEx or RegExp) which is very similar to PCRE (Perl Compatible Regular Expressions), and you can read their documentation by choosing ICU Regular Expression Reference from the Help menu in Keyboard Maestro.

In Keyboard Maestro Actions, if the term “matches” is used, it means that it supports regular expression. However, there are many Actions in Keyboard Maestro where you can use a regular expression, which may not be obvious. For more info see Places where Regular Expressions can be used.


Capture Groups

  • Use the form of $n in the Action Replace box, where n is the number for the capture group.
    1. Example: $1, $2, $3
    2. This is the same as \n used in other apps/languages.

Search Modifiers

The ICU calls these modifiers “flag options”.

The search modifier “Pattern to Use” shown below is placed at the very beginning of the Search/Find Regular Expression box.
For example:
(?m)^\s*\d+[\t]+
as shown in the below example #1.

PurposePattern to UseDescription
GlobalNONEAll searches are global in Keyboard Maestro.
This is often indicated in other tools by the /g modifier.
Case Insensitive(?i)Matching will be in a case-insensitive manner.
In some Actions this is NOT necessary since the Action already provides an “ignoring case” option.
Dot includes EOL(?s)A “.” in a pattern will match a line terminator in the input text. Note that a CR LF pair in text behave as a single line terminator, and will match a single “.” in a RE pattern.
Multi-Line(?m)Controls the behavior of “^” and “$” in a pattern.
If used, “^” and “$” will also match at the start and end of each line within the input text.
Word Boundaries(?w)Controls the behavior of \b in a pattern.
If used, word boundaries are found according to the definitions of word found in Unicode UAX 29, Text Boundaries.
Comments(?x)Ignores white space and allows #comments.
If used, white space within regular expressions is ignored and you can use #line-comments.

Use of Text Tokens

You can use Keyboard Maestro Text Tokens anywhere appropriate in both the Search pattern and the Replace pattern.

Examples

Example #1: Remove all line numbers from a string with multiple lines

Example #2: Extract Capture Group for Multiple Matches to Multiple Lines in a Variable

Sometimes you may need to extract a RegEx Capture Group after a match is made, and do this for multiple lines (matches) in the entire source string. To achieve this, you need to:

  1. Use the For Each Action to get each match into a variable (MatchString)
  2. Then use a Search Variable Action to get the Capture Group for that match

Both Actions can use the same RegEx pattern.


For a complete macro using this method, see:
MACRO: Get List of RegEx Capture Group of Multiple Matches

See also:


Actions

Conditions

Triggers

Macros That Use RegEx

Forum

General

Software

Books

Online References

Online Primers & Tutorials

Regular_Expressions.txt · Last modified: 2017/03/05 22:36 by peternlewis