Backreferences \1 \2
Backreferences let you match the same text that was captured by a previous group. Use \1 for the first group, \2 for the second, etc.
Basic Backreferences
(\w+) \1 matches a word, space, then the SAME word again.
How It Works
(\w+)captures a word into group 1matches a space\1matches whatever group 1 captured
If group 1 captured "hello", then \1 only matches "hello" - not any word.
Exercises
Multiple Backreferences
Practical Examples
Common Patterns
Finding Typos (Doubled Words)
Matching Delimiters
Note: This doesn't ensure matching pairs - for that you'd need:
Practice Playground
Try:
(\w+) \1- doubled words(['"]).*?\1- quoted strings<(\w+)>.*?</\1>- HTML tags
Key Takeaways
\1,\2, etc. reference captured groups- Backreferences match the exact same text
- Useful for finding duplicates and matching pairs
- Groups are numbered by opening
(position - Non-capturing groups
(?:)don't create backreferences

