Groups


When building patterns it is often helpful to group sub-patterns together, for example to use the group in alternation or to apply a quantifier. For example,

A group is created by wrapping the sub-pattern in \%(...). Technically, this creates a non-capturing group, which is slightly different from a capturing group which is discussed in the next section. All information in this section applies to both capturing and non-capturing groups, but this section uses the non-capturing notation because it is more efficient when capturing is not required.

Notation Group Type
\%( ... \) Non-capturing
\( ... \) Capturing

As a simple example, suppose we want to construct a pattern that matches "abcabcabc". Following the discussion in the quantifiers section, we might write:

abc\{3\}

Let's test this pattern using the following buffer:

Initial Conditions
abc
abcabcabc
abcc
abccc
COMMANDTop1:1
/abc\{3\}

First we execute the search

Search without group
abc
abcabcabc
abcc
abccc
NORMAL80%4:1
 

then select the matching text

what happened? The quantifier is applied to the atom on the left, which in this case is "c".

To fix this, we need to create a group around the "abc" so that the entire string is treated as an atom:

\%(abc\)

then apply the quantifier to the group:

\%(abc\)\{3\}

and select the matched text

Select matching text
abc
abcabcabc
abcc
abccc
NORMAL40%2:1
 

which confirms that the updated pattern achieves our goal.