> Why would you expect a single dictionary? You're doing 3 separate matches! > > correct, I didn't knew how to combine it all the patterns
You could just combine the patterns as alternatives: > > # The alternatives are matched repeatedly. The final '.' alternative > # will consume a character if none of the previous subpatterns match, > # ready for the next repeat. > subpatterns = [r'(?P<Block>(\d+,\d+,\d+:\d+))', > r'(?P<p_owner>([0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+::HEAD))', > > r'(?P<a_owner>(owner:\s+[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+::HEAD))', > '.'] > pattern = '(%s)*' % '|'.join(subpatterns) > match = re.search(pattern, line) > print ' ', match.groupdict() > > There seems to be some silly mistake after I added the above code. I get "*sre_constants.error: nothing to repeat "* error Sample code: #!/usr/bin/python import re with open("/tmp/2.repo","r") as f: for line in f: result = re.search(r'MSG=attempt to record(.*)LINSNAP', line) if result: for subpattern in [r'(?P<Block>(\d+,\d+,\d+:\d+))', r'(?P<p_owner>([0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+::HEAD))', r'(?P<a_owner>(owner:\s+[0-9a-fA-F]+:[0-9a-fA-F]+:[0-9a-fA-F]+::HEAD))', '.']: regex = re.compile(subpattern) pattern = '(%s)*' % '|'.join(subpattern) match = re.search(pattern, line) print ' ', match.groupdict() Sample output: yy-1# python final_01.py Traceback (most recent call last): File "final_01.py", line 18, in <module> match = re.search(pattern, line) File "/usr/local/lib/python2.7/re.py", line 146, in search File "/usr/local/lib/python2.7/re.py", line 251, in _compile *sre_constants.error: nothing to repeat* The code looks fine but may be there is something to do with * , may be we need to find a better way to escape * ? > /tmp/final_01.py(17)<module>() -> pattern = '(%s)*' % '|'.join(subpattern) (Pdb) n > /tmp/final_01.py(18)<module>() -> match = re.search(pattern, line) (Pdb) pattern '((|?|P|<|B|l|o|c|k|>|(|\\|d|+|,|\\|d|+|,|\\|d|+|:|\\|d|+|)|))*' (Pdb) n error: error('n...repeat',) Regards, Ganesh -- https://mail.python.org/mailman/listinfo/python-list