Skip to content
Regex

Groups and Capturing

Capture, non-capture, and named groups.

By EZ4Code Team
groupcapturenamed

Code

(abc)         capturing group
(?:abc)       non-capturing group
(a)(b)(c)     groups 1, 2, 3
(?P<name>\w+) named group (Python/PCRE)
(?<word>\w+)  named group (.NET/Java/JS)
(?<word>\w+)\k<word>  named backreference
\g<1>         backref to group 1 (Python)

Explanation

Parentheses create groups, and by default each group captures its match for later reference by number or name. A non-capturing group (?:...) groups without saving a capture, which is faster and avoids shifting group indices. Named groups (?<name>...) improve readability and are referenced by name in replacements and backreferences.

More Regex Snippets