Search code examples
regexinfinite-sequence

Capture indefinite amount of numbers with Regex?


I want to get captures using regex on a string that could contain an indefinite amount of numbers. My intuition lead me to do "/\.getnumbers (\d+)+\s*/"but that only matched the first number following the .getnumbers command. How do I write a regex statement that will capture one or more numbers after the command separated by a simple space. For example: .getnumbers 5 4 3 2 1 would match (5) (4) (3) (2) (1), though the regex isn't specifically written to match 5 numbers, it could match any amount of numbers.


Solution

  • You probably can't do it without postprocessing, since most regex engines don't allow an indefinite number of groups. Fortunately the postprocessing consists only of splitting by spaces.

    /\.getnumbers (\d+(?: \d+)*)/