Recall & Review
beginner
What is a lookahead in regular expressions?
A lookahead checks if a certain pattern exists ahead in the text without including it in the match. It looks forward but does not consume characters.
Click to reveal answer
beginner
What is a lookbehind in regular expressions?
A lookbehind checks if a certain pattern exists behind the current position without including it in the match. It looks backward but does not consume characters.
Click to reveal answer
intermediate
How do you write a positive lookahead in PHP regex?
Use
(?=pattern). For example, /foo(?=bar)/ matches 'foo' only if it is followed by 'bar'.Click to reveal answer
intermediate
How do you write a negative lookbehind in PHP regex?
Use
(?<!pattern). For example, /(?<!foo)bar/ matches 'bar' only if it is NOT preceded by 'foo'.Click to reveal answer
intermediate
Why use lookahead and lookbehind instead of normal matching?
They let you check context around a match without including that context in the matched text. This helps when you want to find a pattern only if it is near another pattern, but you don't want to capture the nearby pattern.Click to reveal answer
Which syntax represents a positive lookahead in PHP regex?
✗ Incorrect
A positive lookahead uses (?=pattern) to check if 'pattern' follows without including it in the match.
What does (?foo)bar match?
✗ Incorrect
(?
Which lookaround checks text after the current position?
✗ Incorrect
Lookahead checks the text ahead (after) the current position.
What is the main difference between lookahead and normal matching?
✗ Incorrect
Lookahead checks for a pattern ahead but does not include it in the matched text, unlike normal matching.
Which of these is NOT a valid lookaround syntax in PHP regex?
✗ Incorrect
(?<>pattern) is not a valid lookaround syntax.
Explain how lookahead and lookbehind help in matching patterns with context in PHP regex.
Think about checking surroundings without including them in the match.
You got /4 concepts.
Write a PHP regex example using positive lookahead and explain what it matches.
Try matching a word only if followed by another word.
You got /3 concepts.