Skip to main content
Workday User Guide
Last Updated: 2023-06-23
Regex Capturing Groups

Regex Capturing Groups

You can use a pair of parentheses around a subpattern in a regular expression to define a
group
. You can use regex groups to:
  • Apply regex operators and quantifiers to an entire group at once.
  • Create a capturing group.
    You can use capturing groups to specify matching values to save or return from your regular expression. By default, a group
    captures
    the text that produces a match. The portion of the string that matches the grouped subexpression is captured in memory for later retrieval or use.
  • Create a non-capturing group.

Group Numbering

A regular expression can have more than 1 group, and the groups can be nested. The groups are numbered 1-
n
from left to right, starting with the first opening parenthesis. There is always an implicit group zero (0), which contains the entire match. Example:
(a(b*))+(c)
This pattern contains 3 groups:
group 1: (a(b*)) group 2: (b*) group 3: (c)

Capturing Groups and the REGEX Function

You can use the
REGEX
function to extract a portion of a string. The
REGEX
function returns the value of the
first
capturing group only.
Suppose you have a field name called
email
that contains email addresses with this pattern:
username
@
provider
.
domain
. You can use the
REGEX
function to return just the
provider
portion of the email address from the
email
field:
REGEX([email],"^[a-zA-Z0-9._%+-]+@
([a-zA-Z0-9._-]+)
\.[a-zA-Z]{2,4}$")

Capturing Groups and the REGEX_REPLACE Function

You can use the
REGEX_REPLACE
function to match a string, and replace matched strings with another value. The
REGEX_REPLACE
function takes 3 arguments:
  • Input string
  • Matching regex
  • Replacement regex
You can use capturing groups to capture backreferences, but the entire match is always returned.

Capturing Groups and Backreferences

You can use a backreference to refer back to the matched content of a particular capturing group number. Typically, you use a backreference in the same regular expression that contains the capturing group. You specify a backreference by referring to the group number preceded by a backslash. Use
\1
to refer to capturing group 1,
\2
to refer to capturing group 2, and so on.
Suppose you want to match a pair of HTML tags and their enclosed text. You could capture the opening tag into a capturing group, and then use a backreference to match the corresponding closing tag:
(
<
([A-Z][A-Z0-9]*)
\b[^>]*>.*?</
\2
>
)
This regular expression contains 2 capturing groups:
  • Group 1 contains the outermost parentheses and captures the entire string.
  • Group 2 captures the string matched by
    [A-Z][A-Z0-9]*
    .
You can then refer to group 2 using the
\2
backreference to match the corresponding closing HTML tag.
When you use the
REGEX_REPLACE
function, you can use a backreference to refer to a capturing group in the
previous
regular expression. The syntax is slightly different when you use a backreference to refer to a group in the previous regex. Use a dollar sign (
$
) before the group number, such as
$1
to specify a backreference to group 1 of the previous expression.
Suppose you have a
phone_number
field where the values are formatted as
xxx.xxx.xxxx
. The following example matches the values in
phone_number
and replaces them with values formatted as
(xxx) xxx-xxxx
:
REGEX_REPLACE([phone_number],"([0-9]{3})\.([[0-9]]{3})\.([[0-9]]{4})","
\($1\) $2-$3
")
Notice the backreferences in the replacement expression. They refer to the capturing groups of the previous matching expression.

Non-Capturing Groups

In some cases, you might want to use parenthesis to group subpatterns, but
not
capture text. A non-capturing group starts with
(?:
(a question mark and colon following the opening parenthesis). For example,
h(?:a|i|o)t
matches
hat
or
hit
or
hot
, but does not capture the
a
,
i
, or
o
from the subexpression.