How to make regex catch multiple numbers instead of only one?


'cubed_intent': r'.*cube.*(\d+)'

In the above regex expression, I have been trying to catch multiple numbers, but it only catches the first one inputted by the user. for instance if the user says:

Cube 78
It will only take the 7 and cube it to be 343
Instead of taking the cube of 78.

Can anyone give me some more insight into it?

The grouping which enables the cubing is fetched from:


Are you sure cube 78 would have the 7 captured instead of the 8? By default, regex matching is “greedy,” so the .* after cube would capture all but the last digit, leaving (\d+) to capture only the last digit. Instead, try this expression: r'\s*cube\s+(\d+)', where \s+ matches one or more whitespace characters, instead of .*, which matches zero or more of any characters. BTW, go to to verify.


Hey @chuckwondo,

I just changed it to 'cubed_intent': r'.*cube (\d+)' and it works now!
I think the issue with r'.*cube.*(\d+)' was the .* before the (\d+) which then only matched the 1 inputted number instead of all of them. Still seems kind of weird tho.

Thanks so much!!
Your solution seems to work too!

1 Like

Yes, as I said the .* after cube (or before (d+) as you phrased it) is greedy, meaning that it will match as much as possible, leaving only 1 digit for (\d+) to match.

While your regex will work in some cases, it is does not work when there is more than one space character between cube and the digits, which is why I specified \s+ between cube and the digits. That allows for 1 or more whitespace characters. For example, your regex will not match cube 78 (2 spaces in between).

Further, because your regex includes .* immediately preceding cube, it will mistakenly match any span of characters that end with cube. For example, it would match hypercube 78. That’s why the regex I provided includes \s* preceding cube, which allows only for zero or more whitespace characters preceding cube.