📖 Scenario: You are working on a PHP script that processes text to find repeated words. This is useful for cleaning up typos or emphasizing repeated words in a sentence.
🎯 Goal: Build a PHP script that uses a regular expression with capturing groups and backreferences to find repeated words in a string.
📋 What You'll Learn
Create a string variable with a sentence containing repeated words.
Create a regular expression pattern using capturing groups to detect repeated words.
Use the pattern with preg_match_all to find all repeated words.
Print the repeated words found.
💡 Why This Matters
🌍 Real World
Detecting repeated words helps clean up text input, improve readability, and catch typing errors automatically.
💼 Career
Understanding capturing groups and backreferences is essential for text processing, data validation, and building search or cleanup tools in PHP.
Progress0 / 4 steps
1
Create the input string
Create a string variable called text with the value "This is is a test test sentence.".
PHP
Hint
Use double quotes for the string and assign it to $text.
2
Create the regex pattern with capturing groups
Create a variable called pattern and assign it the regex string "/\b(\w+) \1\b/" which uses a capturing group and a backreference to find repeated words.
PHP
Hint
The pattern uses \b for word boundaries, (\w+) to capture a word, and \1 to match the same word again.
3
Find all repeated words using preg_match_all
Use preg_match_all with $pattern and $text to find all matches. Store the matches in a variable called matches.
PHP
Hint
Use preg_match_all function with the pattern and text variables, and pass $matches as the third argument to collect results.
4
Print the repeated words found
Print the repeated words found by accessing $matches[0] using print_r.
PHP
Hint
Use print_r($matches[0]) to display the array of repeated words.
Practice
(1/5)
1. What does a capturing group in a PHP regular expression do?
easy
A. It ignores the matched text inside parentheses.
B. It repeats the entire pattern multiple times.
C. It saves part of the matched text for later use.
D. It changes the case of matched text.
Solution
Step 1: Understand capturing groups
Parentheses () in regex save the matched part for reuse.
Step 2: Purpose of capturing groups
This saved part can be accessed later in the code or reused in the pattern.
Final Answer:
It saves part of the matched text for later use. -> Option C
Quick Check:
Capturing group = save matched text [OK]
Hint: Parentheses capture text to reuse later in regex or code [OK]
Common Mistakes:
Thinking capturing groups repeat patterns
Believing they ignore matched text
Assuming they change text case
2. Which of the following is the correct syntax for a backreference to the first capturing group in a PHP regex pattern?
easy
A. \(1\)
B. \\1
C. \$1
D. \1
Solution
Step 1: Identify backreference syntax in PHP regex
Backreferences use a single backslash followed by group number, like \1 in regex patterns.
Step 2: Differentiate from string escaping
In PHP strings, to represent \1 you write "\\1" but the regex pattern itself uses \1.
Final Answer:
\1 -> Option D
Quick Check:
Backreference first group = \1 [OK]
Hint: Use single backslash before group number for backreference in regex [OK]
Common Mistakes:
Using double backslash instead of single
Confusing backreference with variable syntax
Adding parentheses around the number
3. What will be the output of the following PHP code?
The pattern /(\w+)\s\1/ matches a word, a space, then the same word again (backreference \1).
Step 2: Apply pattern to the subject string
The string 'hello hello world' contains 'hello hello' which matches the pattern.
Final Answer:
hello hello -> Option B
Quick Check:
Backreference matches repeated word = hello hello [OK]
Hint: Backreference \1 matches exact previous captured word [OK]
Common Mistakes:
Ignoring backreference meaning
Matching different words instead of repeated
Expecting full string match
4. Identify the error in this PHP regex pattern: preg_match('/(\d+)\s\2/', '123 123', $matches);
medium
A. Backreference \2 is invalid because only one group is captured.
B. The pattern should use \1 instead of \2 for backreference.
C. The pattern is missing delimiters.
D. The pattern should not use capturing groups.
Solution
Step 1: Count capturing groups in the pattern
There is only one capturing group: (\d+), so only \1 is valid.
Step 2: Check backreference usage
Using \2 refers to a second group which does not exist, causing an error.
Final Answer:
Backreference \2 is invalid because only one group is captured. -> Option A
Quick Check:
Backreference number must match captured groups [OK]
Hint: Backreference number must not exceed captured groups count [OK]
Common Mistakes:
Using backreference number higher than groups
Ignoring missing groups
Confusing delimiters with backreferences
5. Given the string 'abc123abc456', which regex pattern using capturing groups and backreferences will match the repeated substring 'abc' only if it appears twice separated by digits?
hard
A. /(abc)\d+\1/
B. /(abc)\1\d+/
C. /abc(\d+)abc/
D. /(abc)+\d+/
Solution
Step 1: Understand the requirement
We want to match 'abc' repeated twice with digits in between.
Step 2: Analyze each pattern
/(abc)\d+\1/: /(abc)\d+\1/ matches 'abc', digits, then same 'abc' again. This fits the requirement. /(abc)\1\d+/: /(abc)\1\d+/ expects 'abcabc' then digits, which is incorrect. /abc(\d+)abc/: /abc(\d+)abc/ matches 'abc', digits, 'abc' but digits are captured, not 'abc'. /(abc)+\d+/: /(abc)+\d+/ matches one or more 'abc' then digits, but no backreference to ensure repetition.
Final Answer:
/(abc)\d+\1/ -> Option A
Quick Check:
Backreference \1 repeats captured 'abc' after digits [OK]
Hint: Backreference \1 repeats first group exactly where needed [OK]
Common Mistakes:
Placing backreference before digits
Not using backreference to repeat 'abc'
Capturing digits instead of 'abc'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.