parse commands with regex and python
Question: I have a string like this:

    
    
    str = "something move 11 something move 12 something 13 copy 14 15"
    

_where the "something" means some text, or no text at all._  
and as a result I want to have a list like:

    
    
    [('move', 11, ''), ('move', 12, 13), ('copy', 14, 15)]
    

I tried using this:

    
    
    re.findall('(move|copy).+?([0-9]+).+?([0-9]+)*', str)
    

but it gives my the output:

    
    
    [('move', 11, ''), ('move', 12, ''), ('copy', 14, '')]
    

I understand that is because the last number is optional, but I just have no
idea how I could it get working.

How can I do this?


Answer: You could use a [regular expression](http://docs.python.org/2/library/re.html)
(with lookbehind and lookahead):

    
    
    In [1]: import re
    
    In [2]: tokens = "something move 11 something move 12 something 13 copy 14 15"
    
    In [3]: split_movements = re.split('(?<=\d)\s(?!\d+)', tokens)
    
    In [4]: split_movements
    Out[4]: ['something move 11', 'something move 12', 'something 13', 'copy 14 15']
    
    In [5]: movements = [re.split('\s(?=\d+)', m) for m in split_movements]
    
    In [6]: movements
    Out[6]: 
    [['something move', '11'],
     ['something move', '12'],
     ['something', '13'],
     ['copy', '14', '15']]
    

