Some samples inputs
winning numbers: 1 winning numbers: 1,2,3,4 winning numbers: 1,23, 28, 273, 191
Desired matches
[1] [1,2,3,4] [1,23,28,273,191]
It is a simple pattern, but I’m not really sure how to match all of the numbers. I was thinking something like “get the first number, then zero or more numbers preceded by a comma and possibly spaces”
winning numbers:\s*(\d+)\s*(,\s*(\d+))*
But it only matches the first one (as expected), and the last number.
I’m using ruby, so I want to be able to retrieve all of the matches in an array and pass it off. With my current regex, it matches the last number, but it also matches the comma as well cause it’s inside the parentheses.
Is my logic flawed? Or am I not expressing it as a regex correctly?
I’m using rubular to test my regex.
You can use
scanmethod to match all numbers and thenmapthem into the array with converting each one to integer usingto_i