Subexpressions: Remembering patterns in RegExpr

It's often useful to remember patterns that have been matched so that they can be used again. Subexpressions are used for this purpose.

Subexpressions look like this: (expression)

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

Example

Subexpressions are a useful way to extract and save several parts of the input at a time.

Example: Assume the following input: "Let a = x - 1". Regular expression "Let (\w+) = (.+)" will match the input. It has two subexpressions. Subexpression 1 will be "a" and subexpressions 2 will be "x - 1".

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

Backreferences

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

Example: Expression (\w+) \1 matches any word followed by a space and the same word again.

Backreferences work only after the subexpression has first been matched. That means a backreference must be located somewhere after the corresponding (expression).

Good to know

Subexpressions can also be used for string substitution. For the details see RegExprSubst.

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 · RegExpr Help