1. Help Center
  2. Build
  3. Regular Expressions (Regex)

Grouping

In regular expressions, parentheses create groupings. When you enter search text, you can create a group within parentheses to constrain the alternation character; the result is that the "or" applies only to what is within the parenthesis.

In the example, th(is|at|ose), the parenthesis surround "is|at|ose". This regex is the same as saying "this or that or those," but in a more efficient manner. When testing the search text for matches against the content, SiteSpect first looks for the "th" and then it looks for one of "is" or "at" or "ose" following the "th" that it already found. If you were trying to match this|that|those without using grouping, SiteSpect would have to start its comparison process from the beginning of the content for each member of the group. Using grouping, SiteSpect searches the content only once.

As a second example, to match the books category, movies category, or electronics category pages you might use a pattern similar to /category/(books|movies|electronics), which looks for a "/category/" directory in the URL followed by either "books", "movies", or "electronics".

Constraining alternations with groupings also works in Search & Replace. However, in a Search & Replace operation, groupings in the search text are primarily used to capture, or remember, the text matched by the pattern within the parenthesis for later use in the replacement text. Groupings and wildcards are frequently used together because they allow you to match and remember all of the text matched by the wildcard.

In the replacement pattern, you can reference the text in the capture groups using capture group variables, which is like a variable in computer programing. In SiteSpect, capture group variables are sequential and have the form "$#"; a dollar sign followed by a number. The number of a capture group is determined by the count of open parenthesis in capture groupings. The first capture group is stored in $1 and the second $2 and so on.

Capture groups are read left to right. This means that in the following expression...

(I am) (reading the (SiteSpect) user guide.) (I am) (reading the (SiteSpect) user guide.)

the capture groups are numbered as follows:

$1 = I am

$2 = reading the SiteSpect user guide.

$3 = SiteSpect

For example, if you want to add the message "(not including tax)" to the subtotal of the cart page, you would search for the subtotal on a page by using the search pattern (Subtotal.*?)\$. When this pattern matches the text matched by Search.*?, it is stored in the capture group variable $1 because it is the first capture group in the search pattern. Every character matched by .*? within the capture group is stored in $1 in addition to the literal text "Subtotal". To insert the message directly before the dollar sign, use a replacement pattern like $1 (not including tax) \$. Note that the parentheses in replacement patterns are not special characters so they do not create additional capture groups. There are only two characters that are special in the replacement text; the dollar sign “$”, and the backslash “\”. Both of these need to be escaped if they are intended to be literal. The dollar sign is a special character in regular expressions because it is used to indicate a capture group variable. Any dollar sign followed by a number is replaced by a capture group variable (if present). To avoid this, escape all dollar values in the replacement text.