I'm making a simple stack-based language which uses commands to manipulate the stack. When I find a command in the source, I use this regex to separate out the actual command name, such as sum, and the arguments to the command. Arguments are surrounded by triangle brackets and are separated by commas.
Here's the regex I'm currently using:
(?<command>[^<>\s]+)(\<(?<args>(\d+)+(?>,\s*\d+)*)\>)?
Now this works fine, and here are some examples of it working:
+ => command: '+', args: nil
sum<5> => command: 'sum', args: '5'
print<1, 2, 3> => command: 'print', args: '1, 2, 3'
This works exactly as I want for each one but the last. My question is, is there a way to capture each argument separately? I mean like this:
print<1, 2, 3> => command: 'print', args: ['1', '2', '3']
By the way, I'm using the latest Ruby regex engine.