Introduction

A regular expression describes strings of characters. It is a sequence of characters (called a pattern) that matches certain strings in a text.
This document is not a tutorial about regular expressions (there are plenty around). It is a quick reference for the various elements which are used to build up a regular expression pattern. The
Find dialog in Alpha and the [search] command support this syntax.
Alpha uses the regular expression engine provided by Cocoa, which is itself based on the ICU's Regular Expressions package.
The ICU regular expressions are described at https://userguide.icu-project.org/strings/regexp.

Quick reference

The following tables are reproduced from the ICU User Guide (see the ICU License terms).
They describe the character expressions used by the regular expression to match patterns within a string, the pattern operators that specify how many times a pattern is matched and additional matching restrictions, and the last table specifies flags that can be included in the regular expression pattern that specify search behavior over multiple lines

Regular Expression Metacharacters

The following table describes the character sequences used to match characters within a string.
Character ExpressionDescription
\aMatch a BELL, \u0007
\AMatch at the beginning of the input. Differs from ^ in that \A will not match after a new line within the input.
\b, outside of a [Set]Match if the current position is a word boundary. Boundaries occur at the transitions between word (\w) and non-word (\W) characters, with combining marks ignored.
\b, within a [Set]Match a BACKSPACE, \u0008.
\BMatch if the current position is not a word boundary.
\cXMatch a control-X character
\dMatch any character with the Unicode General Category of Nd (Number, Decimal Digit.)
\DMatch any character that is not a decimal digit.
\eMatch an ESCAPE, \u001B.
\ETerminates a \Q ... \E quoted sequence.
\fMatch a FORM FEED, \u000C.
\GMatch if the current position is at the end of the previous match.
\nMatch a LINE FEED, \u000A.
\N{UNICODE CHARACTER NAME}Match the named character.
\p{UNICODE PROPERTY NAME}Match any character with the specified Unicode Property.
\P{UNICODE PROPERTY NAME}Match any character not having the specified Unicode Property.
\QQuotes all following characters until \E.
\rMatch a CARRIAGE RETURN, \u000D.
\sMatch a white space character. White space is defined as [\t\n\f\r\p{Z}].
\SMatch a non-white space character.
\tMatch a HORIZONTAL TABULATION, \u0009.
\uhhhhMatch the character with the hex value hhhh.
\UhhhhhhhhMatch the character with the hex value hhhhhhhh. Exactly eight hex digits must be provided, even though the largest Unicode code point is \U0010ffff.
\wMatch a word character. Word characters are [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}].
\WMatch a non-word character.
\x{hhhh}Match the character with hex value hhhh. From one to six hex digits may be supplied.
\xhhMatch the character with two digit hex value hh.
\XMatch a Grapheme Cluster.
\ZMatch if the current position is at the end of input, but before the final line terminator, if one exists.
\zMatch if the current position is at the end of input.
\nBack Reference. Match whatever the nth capturing group matched. n must be a number > 1 and < total number of capture groups in the pattern.
\0oooMatch an Octal character. ooo is from one to three octal digits. 0377 is the largest allowed Octal character. The leading zero is required; it distinguishes Octal constants from back references.
[pattern]Match any one character from the pattern.
.Match any character. See the s character expression in the the Flag Options table.
^Match at the beginning of a line. See the m character expression in the Flag Options table.
$Match at the end of a line. See the m character expression in the Flag Options table.
\Quotes the following character. Characters that must be quoted to be treated as literals are * ? + [ ( ) { } ^ $ | \ . /

Regular Expression Operators

The following table defines the regular expression operators.
OperatorDescription
|Alternation. A|B matches either A or B.
*Match 0 or more times. Match as many times as possible.
+Match 1 or more times. Match as many times as possible.
?Match zero or one times. Prefer one.
{n}Match exactly n times.
{n,}Match at least n times. Match as many times as possible.
{n,m}Match between n and m times. Match as many times as possible, but not more than m.
*?Match 0 or more times. Match as few times as possible.
+?Match 1 or more times. Match as few times as possible.
??Match zero or one times. Prefer zero.
{n}?Match exactly n times.
{n,}?Match at least n times, but no more than required for an overall pattern match.
{n,m}?Match between n and m times. Match as few times as possible, but not less than n.
*+Match 0 or more times. Possessive Match.
++Match 1 or more times. Possessive match.
?+Match zero or one times. Possessive match.
{n}+Match exactly n times.
{n,}+Match at least n times. Possessive Match.
{n,m}+Match between n and m times. Possessive Match.
(...)Capturing parentheses. Range of input that matched the parenthesized subexpression is available after the match.
(?: ... )Non-capturing parentheses. Groups the included pattern, but does not provide capturing of matching text.
(?> ... ) Atomic-match parentheses. First match of the parenthesized subexpression is the only one tried; if it does not lead to an overall pattern match, back up the search for a match to a position before the opening parenthesis.
(?# ... )Free-format comment (?# comment ).
(?= ... )Look-ahead assertion. True if the parenthesized pattern matches at the current input position, but does not advance the input position.
(?! ... )Negative look-ahead assertion. True if the parenthesized pattern does not match at the current input position. Does not advance the input position.
(?<= ... )Look-behind assertion. True if the parenthesized pattern matches text preceding the current input position, with the last character of the match being the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.)
(?<! ... )Negative Look-behind assertion. True if the parenthesized pattern does not match text preceding the current input position, with the last character of the match being the input character just before the current position. Does not alter the input position. The length of possible strings matched by the look-behind pattern must not be unbounded (no * or + operators.)
(?ismwx-ismwx:...)Flag settings. Evaluate the parenthesized expression with the specified flags enabled or disabled (if preceded by the minus sign). The flags are defined in the Flag Options table.
(?ismwx-ismwx)Flag settings. Change the flag settings. Changes apply to the portion of the pattern following the setting. For example, (?i) changes to a case insensitive match.The flags are defined in the Flag Options table.

Flag Options

The following flags control various aspects of regular expression matching. These flag values may be specified within the pattern using the (?ismwx-ismwx) pattern options. The minus sign - is used to disable a flag. For instance, (?ix-m) means enable the i and x flags and disable the m flag.
FlagDescription
iIf set, matching will take place in a case-insensitive manner.
mControl the behavior of ^ and $ in a pattern.
sIf set, a . in a pattern will match a line terminator in the input text. By default, it will not.
wControls the behavior of \b in a pattern.
xIf set, allow use of white space and #comments within patterns

By default, ^ and $ will only match at the start and end, respectively, of the input text. If the m flag is set, ^ and $ will also match at the start and end of each line within the input text.
By default, the
[search] command always sets this flag when performing a regular expression search. It this is not the desired behavior, you must explicitely specify the (?-m) flag at the beginning of your pattern to remove the option or set the -ml option.
For the s flag, line terminators are \u000a, \u000b, \u000c, \u000d, \u0085, \u2028, \u2029 and the sequence \u000d\u000a. Note that a CRLF in text behave as a single line terminator, and will match a single . in a regular expression pattern.
If the w flag is set, word boundaries are found according to the definitions of word found in Unicode UAX 29, Unicode Text Segmentation. By default, word boundaries are identified by means of a simple classification of characters as either word or non-word, which approximates traditional regular expression behavior. The results obtained with the two options can be quite different in runs of spaces and other non-word characters.