A lookahead assertion tests what follows the current position without consuming those characters. A positive lookahead uses (?=...), while a negative lookahead uses (?!...). section 11.5.3
Match numbers followed by a period
Given str = "123 456. 789", the pattern /\d+(?=\.)/ matches 456. The period satisfies the assertion but remains outside the match, so it can still be matched by later pattern components. section 11.5.3
Keep these mechanics in mind:
- Treat the assertion as zero-width: checking the next characters does not advance the match position. section 11.5.3
- Do not expect the lookahead parentheses to create a capture; the asserted text does not populate $1. section 11.5.3
- Contrast lookahead with lookbehind, which checks preceding text instead of following text. section 11.5.3
str = "123 456. 789"
m = /\d+(?=\.)/.match(str)
Run the compact Ruby example to inspect which number sequence matches while the period remains unconsumed. section 11.5.3
