📖 Scenario: You are working with text data and want to find specific patterns using PHP regular expressions. Lookahead and lookbehind help you find matches based on what comes before or after a pattern without including those parts in the match.
🎯 Goal: Build a PHP script that uses lookahead and lookbehind in regular expressions to find words that meet certain conditions in a sentence.
📋 What You'll Learn
Create a string variable with a sentence containing multiple words.
Create a pattern using positive lookahead to find words followed by a specific character.
Create a pattern using positive lookbehind to find words preceded by a specific character.
Print the matched words for both patterns.
💡 Why This Matters
🌍 Real World
Lookahead and lookbehind are useful in text processing, such as searching for words that appear before or after certain characters without including those characters in the result.
💼 Career
Understanding these regex features helps in jobs involving data cleaning, validation, and complex text searches, common in web development and data analysis.
Progress0 / 4 steps
1
Create the sentence string
Create a string variable called sentence with the exact value: "apple banana cherry date".
PHP
Hint
Use $sentence = "apple banana cherry date"; to create the string.
2
Create a lookahead pattern
Create a variable called pattern_lookahead with the regex pattern that matches words followed by a space and the letter c using positive lookahead: /\b\w+(?= c)/.
PHP
Hint
Use $pattern_lookahead = '/\b\w+(?= c)/'; to create the lookahead pattern.
3
Create a lookbehind pattern
Create a variable called pattern_lookbehind with the regex pattern that matches words preceded by the letter a and a space using positive lookbehind: /\b(?<=a )\w+/.
PHP
Hint
Use $pattern_lookbehind = '/\b(?<=a )\w+/'; to create the lookbehind pattern.
4
Find and print matches
Use preg_match_all with $pattern_lookahead and $pattern_lookbehind on $sentence to find matches. Store matches in $matches_lookahead and $matches_lookbehind. Then print the matches using print_r for both variables.
PHP
Hint
Use preg_match_all($pattern_lookahead, $sentence, $matches_lookahead); and preg_match_all($pattern_lookbehind, $sentence, $matches_lookbehind); then print with print_r($matches_lookahead[0]); and print_r($matches_lookbehind[0]);.
Practice
(1/5)
1. What does a positive lookahead (?=pattern) do in PHP regular expressions?
easy
A. Matches the pattern and consumes it in the string
B. Checks if the specified pattern precedes the current position and includes it in the match
C. Checks if the specified pattern follows the current position without including it in the match
D. Checks if the pattern is anywhere in the string regardless of position
Solution
Step 1: Understand lookahead behavior
A positive lookahead (?=pattern) checks if the pattern appears immediately after the current position without consuming characters.
Step 2: Compare options with definition
Checks if the specified pattern follows the current position without including it in the match correctly describes this behavior. Options B and C incorrectly describe lookbehind or normal matching. Checks if the pattern is anywhere in the string regardless of position is too broad.
Final Answer:
Checks if the specified pattern follows the current position without including it in the match -> Option C
Quick Check:
Positive lookahead = check after position [OK]
Hint: Lookahead checks after current spot without consuming [OK]
Common Mistakes:
Confusing lookahead with lookbehind
Thinking lookahead consumes characters
Assuming it matches anywhere, not just after position
2. Which of the following is the correct syntax for a negative lookbehind in PHP regex?
easy
A. (?pattern)
B. (?<=pattern)
C. (?!pattern)
D. (?=pattern)
Solution
Step 1: Recall lookbehind syntax
Negative lookbehind in PHP regex is written as (?<!pattern), which asserts that the pattern does NOT appear before the current position.
Step 2: Match options to syntax
(?
Final Answer:
(?<!pattern) -> Option A
Quick Check:
Negative lookbehind = (?<!pattern) [OK]
Hint: Lookbehind uses (?<=) or (?), negative has exclamation [OK]
Common Mistakes:
Mixing lookahead and lookbehind syntax
Using wrong brackets or missing exclamation mark
Confusing negative and positive forms
3. What will be the output of this PHP code?
$text = "apple pie";
if (preg_match('/apple(?= pie)/', $text)) {
echo "Match found";
} else {
echo "No match";
}
medium
A. Empty output
B. Match found
C. Syntax error
D. No match
Solution
Step 1: Understand the regex pattern
The pattern /apple(?= pie)/ matches 'apple' only if it is immediately followed by ' pie' without including ' pie' in the match.
Step 2: Check the input string
The string is "apple pie", so 'apple' is followed by ' pie'. The lookahead condition is true, so the match succeeds.
Final Answer:
Match found -> Option B
Quick Check:
Lookahead confirms 'apple' before ' pie' = Match found [OK]
Hint: Lookahead checks next chars without consuming [OK]
Common Mistakes:
Thinking lookahead consumes characters
Assuming no match because ' pie' is not part of match
Confusing output with syntax error
4. Identify the error in this PHP regex using lookbehind:
$text = "hello world";
if (preg_match('/(?
medium
A. No error, code runs fine
B. Lookbehind syntax is incorrect, missing brackets
C. Lookbehind must be at the end of the pattern
D. Lookbehind pattern is fixed width but 'hello' is variable length
Solution
Step 1: Check lookbehind syntax and pattern
The lookbehind (?<!hello) is valid because 'hello' is fixed length (5 characters), which PHP supports.
Step 2: Analyze the string and pattern match
The string is "hello world". The lookbehind asserts 'world' is NOT preceded by 'hello'. Here, 'world' is preceded by 'hello', so the match fails and "No match" is printed.
Final Answer:
No error, code runs fine -> Option A
Quick Check:
Lookbehind syntax correct, no error [OK]
Hint: Lookbehind must be fixed width, 'hello' is fixed length [OK]
Common Mistakes:
Thinking 'hello' is variable length
Assuming lookbehind syntax is wrong
Expecting match when lookbehind condition fails
5. You want to match all occurrences of the word 'cat' only if it is NOT preceded by 'wild' and is immediately followed by 's' in a string. Which regex pattern correctly uses lookahead and lookbehind in PHP?
hard
A. /(?<=wild )cat(?=s)/
B. /(?<=wild)cat(?=s)/
C. /(?wild)cat(?= s)/
D. /(?wild )cat(?=s)/
Solution
Step 1: Understand the requirements
We want 'cat' that is NOT preceded by 'wild ' (note the space) and IS followed immediately by 's'.
Step 2: Analyze each option
/(?(?<!wild ) to exclude 'wild ' before 'cat' and positive lookahead (?=s) to ensure 'cat' is followed by 's'. This matches the requirement. /(?<=wild )cat(?=s)/ uses positive lookbehind, which requires 'wild ' before 'cat', opposite of requirement. /(?/(?<=wild)cat(?=s)/ uses positive lookbehind without space, which is incorrect.