Subexpressions: Remembering patterns in RegExpr

Back to help contents

It's often useful to remember patterns that have been matched so that they can be used again. Subexpressions are used for this purpose. They are also useful for string substitution (see RegExprSubst).

Subexpressions look like this: (expression)

Anything matched inside the parentheses gets remembered, and can be fetched with a call to RegExprResult.

Subexpressions are a useful way to match several parts of the input at the same time. Example:

Give RegExpr text "Let a = x - 1"

Let (\w+) = (.+)will match the above text, and save "a" as subexpression 1, and "x - 1" as subexpression 2.

You can now retrieve the subexpressions by calling RegExprResult(1) and RegExprResult(2).


You can also match a subexpression in the same regular expression. This is useful for finding duplicate strings. Use the \digit syntax to match the digith subexpression. This is called backreferencing. Example:

(\w+) \1 matches any repeated word with a space between them

Backreferences work only after the subexpression has first been matched, that is, a backreference must be located after the corresponding ( )

Good to know

A total of 31 subexpressions can be specified. However, only 9 backreferences are available (\1 ... \9).

If you don't want to save a particular match, you can use the (?:expression) syntax. This is similar to (expression), but the match is not saved.

Regular expression syntax rules
VB functions in RegExpr

©Aivosto Oy