The contents of capturing groups can be referenced in the pattern using \N
, where N is the group number or by \k<name>
.
The common use cases for backreferencing are:
Let's take an example to understand this feature. In the following panel, we take up a sentence and find and match all the repeated words in it. We capture each word using \b(\w+)\s+
and then refer the contents of the capturing group (\w+)
using \1
later in the pattern to identify the duplicates.
I really appreciate it it if someone explains how the the engine process that pattern pattern step-by-step.
match | position | $1 |
---|---|---|
it it | 20 | it |
the the | 50 | the |
pattern pattern | 78 | pattern |
Using \k
Let's compose regexes to match (single or double) quoted strings. It should match both the variants.
Let's start with [''"](.*?)[''"]
. But it could lead to incorrect matches in texts, where both the single and double quotes are present, like in the text "Fasten your seatbelts. It's going to be a bumpy night."
Here the match starts at "
and stops at '
.
"Fasten your seatbelts. It's going to be a bumpy night." - All About Eve, 1950. "Frankly, my dear, I don't give a damn." - Gone With the Wind, 1939. 'Show me the money!' - Jerry Maguire, 1996. "There's no place like home." - The Wizard of Oz, 1939
match | position | $1 |
---|---|---|
"Fasten your seatbelts. It' | 0 | Fasten your seatbelts. It |
" - All About Eve, 1950. " | 55 | - All About Eve, 1950. |
't give a damn." | 104 | t give a damn. |
'Show me the money!' | 149 | Show me the money! |
"There' | 193 | There |
To make sure that the pattern matches the exact quote at both ends, we can capture and backreference it as given below. In this case, we used a name-based backreferencing using the name quote
.
"Fasten your seatbelts. It's going to be a bumpy night." - All About Eve, 1950. "Frankly, my dear, I don't give a damn." - Gone With the Wind, 1939. 'Show me the money!' - Jerry Maguire, 1996. "There's no place like home." - The Wizard of Oz, 1939
match | position | $1 | $2 | quote |
---|---|---|---|---|
"Fasten your seatbelts. It's going to be a bumpy night." | 0 | " | Fasten your seatbelts. It's going to be a bumpy night. | " |
"Frankly, my dear, I don't give a damn." | 80 | " | Frankly, my dear, I don't give a damn. | " |
'Show me the money!' | 149 | ' | Show me the money! | ' |
"There's no place like home." | 193 | " | There's no place like home. | " |
We can achieve the same output with number-based backreferencing.
"Fasten your seatbelts. It's going to be a bumpy night." - All About Eve, 1950. "Frankly, my dear, I don't give a damn." - Gone With the Wind, 1939. 'Show me the money!' - Jerry Maguire, 1996. "There's no place like home." - The Wizard of Oz, 1939
match | position | $1 | $2 |
---|---|---|---|
"Fasten your seatbelts. It's going to be a bumpy night." | 0 | " | Fasten your seatbelts. It's going to be a bumpy night. |
"Frankly, my dear, I don't give a damn." | 80 | " | Frankly, my dear, I don't give a damn. |
'Show me the money!' | 149 | ' | Show me the money! |
"There's no place like home." | 193 | " | There's no place like home. |