Image for Backreferences

Backreferences

Backreferences are a feature in regular expressions that allow you to reuse parts of a pattern you've already matched. When you identify a certain sequence (like a word or number), you can refer back to it later in the same pattern to find related or repeated information. This is useful for matching repeated words or symmetrical patterns. For example, in the pattern `(word) \1`, the `\1` refers back to what was matched by `(word)` earlier, ensuring the same word appears twice in a row. Backreferences help create more flexible and precise search patterns.