Regex Groups

Capturing and non-capturing groups

Capturing Groups

(abc) # capturing group
\1 # backreference to group 1
\2 # backreference to group 2

Non-Capturing Groups

(?:abc) # non-capturing group
# groups content but doesn not capture

Named Groups

(?abc) # named capturing group
(?Pabc) # named group (Python)

Examples

(\d{3})-(\d{3})-(\d{4}) # captures phone number parts
(\w+)\s+\1 # matches repeated word
(?:https?|ftp):// # matches protocol without capturing