I'm trying to understand the behavior of regex
when using \d
and \w
consecutively to match words and numbers in a sentence. I searched for similar questions but I couldn't find a good match (please let me know if this is somehow duplicate).
# Example sentence
"Adam has 100 friends. Bill has 23 friends. Cindy has 5 friends."
When I use regex [A-Za-z]+\s\w+\s\d+\w
, it returns matches for:
Adam has 100
Bill has 23
BUT NOT FOR
Cindy has 5
I would have expected no matches at all since the greedily searched digits (\d+
) are not followed by any word character (\w
); they are followed by a white space instead. I think, somehow \w
is matching digits following the first occurrence of any digit. I thought \d+
would have exhausted the stretch of digits in the search. Can you help me understand what is going on here?
Thanks