confusion about ruby handling of start and end of pattern

In the case below where a parrent is to be extracted from a string via a regular expression
input_string = "02150 ESPOO"
input_string[pattern]
Two very different result emerge whether the pattern
regex is bookended (starting and finishing) /^\d{5}$/
or
not /\d{5}/
I get the desired result in the latter case, while the former returns nil
.
Why does the bookeneded regex fail?
Answer
The first expression /^\d{5}$/
starts with ^
(which matches the start of a line) and $
(which matches the end of a line). The whole expression can be described as: Match a string, that
- starts a new line
- contains exactly 5 digits only
- ends the line.
This string doesn't match the string you provided, because the string includes two segments one with 5 digits at the start of the string but then there is a unmatched whitespace and 5 letters before the end of the line.
You may try /^\d{5}/
instead (which matches 5 digits at the start of a line).
Enjoyed this article?
Check out more content on our blog or follow us on social media.
Browse more articles