re.sub in Python 3.3
Question: I am trying to change the text string from the form of `file1` to `file01`. I
am really new to python and can't figure out what should go in 'repl' location
when trying to use a pattern. Can anyone give me a hand?

    
    
    text = 'file1 file2 file3'
    
    x = re.sub(r'file[1-9]',r'file\0\w',text) #I'm not sure what should go in repl.
    


Answer: You could try this:

    
    
    >>> import re    
    >>> text = 'file1 file2 file3'
    >>> x = re.sub(r'file([1-9])',r'file0\1',text)
    'file01 file02 file03'
    

The brackets wrapped around the `[1-9]` captures the match, and it is the
first match. You will see I used it in the replace using `\1` meaning the
first catch in the match.

Also, if you don't want to add the zero for files with 2 digits or more, you
could add `[^\d]` in the regexp:

    
    
    x = re.sub(r'file([1-9](\s|$))',r'file0\1',text)
    

