开发者

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开发者_如何学C*/"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.


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+)*)/


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

Note that you'll get all of the numbers as a single capture group. eg: "5 4 3 2 1"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜