Regex Groups and Capturing

Capture, name, and backreference parts of a match.

  • regex groups
  • named group
  • backreference
  • non-capturing
  • regex replace

Regex Groups and Capturing

Capture, name, and backreference parts of a match.

Commands

CommandWhat it does
(\d+)-(\d+)Two numbered groups
(?<year>\d{4})Named group called year
(?:...)Non-capturing group
\1Backreference to group 1
\k<year>Backreference to a named group

Example

(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})

FAQ

When should I use non-capturing groups?

For pure grouping with alternation or quantifiers - it keeps group numbers stable and matching fast.

How do replacements reference groups?

Dollar-1 in JS and backslash-1 in Python; named groups read best in complex rewrites.

FAQ

When should I use non-capturing groups?
For pure grouping with alternation or quantifiers - it keeps group numbers stable and matching fast.
How do replacements reference groups?
Dollar-1 in JS and backslash-1 in Python; named groups read best in complex rewrites.

Regex