Appearance
Grouping Constructs
Capturing groups create a sub-expression by placing an expression inside matching open and close parentheses. Quantifiers and optionality can be applied to sub-expressions. Each set of parentheses creates a capture number that is used to identify capturing groups, ranging from 1 to 9 (\1
to \9
). Capturing groups can be referenced later by their capture number in the regular expression or the replacement string in REGEXP_REPLACE.
Note: While only 9 can be referenced, Yellowbrick supports patterns that contain up to 16 capturing groups. Using more than 16 capturing groups will result in an error.
Non-capturing groups create a sub-expression without creating a capturing group. Non-capturing groups are indicated by including the question mark ( ?
) and colon ( :
) after the opening parenthesis.
Expression | Description |
---|---|
(re) | re is a sub-expression and noted as a capturing group |
(?:re) | re is a sub-expression in the non-capturing group |
Note: It is recommended that capturing groups not be used when the group is the entire expression as this will only cause the engine to execute slower.
Examples
premdb=# select REGEXP_REPLACE('Hello World','(?:\w+) (?:\w+)','\& -> \1') from sys.const;
regexp_replace
-----------------
Hello World ->
(1 row)
premdb=# select REGEXP_REPLACE('Hello World','(\w+) (?:\w+)','\& -> \1') from sys.const;
regexp_replace
----------------------
Hello World -> Hello
(1 row)
premdb=# select REGEXP_REPLACE('Hello World','(?:\w+) (\w+)','\& -> \1') from sys.const;
regexp_replace
----------------------
Hello World -> World
(1 row)
Parent topic:Regular Expression Details