Python regex and using s/ in pattern
Question: I have this regex pattern that when I use in vim works great:

    
    
    s/\.[A-Za-z0-9_]*\(IPROC\|IFIX\|IPTAT\)[A-Za-z_]*\([0-9][0-9]*\)[^0-9]*.*([A-Za-z0-9_]*\(IPROC\|IFIX\|IPTAT\)[A-Za-z_]*\([0-9][0-9]*\)[^0-9]*.*)/\3_\4 
    

I am searching for things like
`.jalsdkjflkajsdf_lajsdlfIFIX_100(IFIX_asdf_200)`

It will return: `IFIX_200` (the last part). I can also set it to return
buffers 1 and 2 so I can get `IFIX_100` (the first part).

How can I use this reg expression in python to return what is expected. I've
tried each of the re.findall and re.search, and no luck.

    
    
    patternI1 = 's/\.[A-Za-z0-9_]*\(IPROC|IFIX|IPTAT\)[A-Za-z_]*\([0-9][0-9]*\)[^0-9]*.*([A-Za-z0-9_]*\(IPROC|IFIX|IPTAT\)[A-Za-z_]*\([0-9][0-9]*\)[^0-9]*.*)/\3_\4'
    with open(filename) as input_file:
      for num, line in enumerate(input_file, 1):
        if re.search(patternI1, line):
          x = re.findall(patternI1, line)
        print x
    


Answer: Leave of the `s/ ... /3_4`; that's Vim search-and-replace syntax. Vim also
uses metacharacters differently, do _not_ escape the `(..)` parenthesis of a
group, but _do_ escape literal `()` parenthesis.

`\w` is a nice shortcut for `[A-Za-z0-9_]`, and `\d` will do for `[0-9]`, `\D`
for `[^0-9]`, using `\d+` where `\d\d*` was used:

    
    
    patternI1 = r'\.\w*(IPROC|IFIX|IPTAT)\w*?(\d+)\D*.*\(\w*(IPROC|IFIX|IPTAT)\w*?(\d+)\D*.*\)'
    

I've adjusted the greedyness of the `\w*` pattern before the digits groups to
prevent these from swallowing too many digits too. Demo:

    
    
    >>> import re
    >>> sample = '.jalsdkjflkajsdf_lajsdlfIFIX_100(IFIX_asdf_200)'
    >>> patternI1 = r'\.\w*(IPROC|IFIX|IPTAT)\w*?(\d\d*)\D*.*\(\w*(IPROC|IFIX|IPTAT)\w*?(\d\d*)\D*.*\)'
    >>> re.search(patternI1, sample).groups()
    ('IFIX', '100', 'IFIX', '200')
    

