This is a pretty straight forward attempt. I haven’t been using python for too long. Seems to work but I am sure I have much to learn. Someone let me know if I am way off here. Needs to find patterns, write the first line which matches, and then add a summary message for remaining consecutive lines which match pattern and return modified string.
Just to be clear…regex .*Dog.* would take
Cat Dog My Dog Her Dog Mouse
and return
Cat Dog ::::: Pattern .*Dog.* repeats 2 more times. Mouse #!/usr/bin/env python # import re import types def remove_repeats (l_string, l_regex): '''Take a string, remove similar lines and replace with a summary message. l_regex accepts strings and tuples. ''' # Convert string to tuple. if type(l_regex) == types.StringType: l_regex = l_regex, for t in l_regex: r = '' p = '' for l in l_string.splitlines(True): if l.startswith('::::: Pattern'): r = r + l else: if re.search(t, l): # If line matches regex. m += 1 if m == 1: # If this is first match in a set of lines add line to file. r = r + l elif m > 1: # Else update the message string. p = '::::: Pattern '' + t + '' repeats ' + str(m-1) + ' more times.\n' else: if p: # Write the message string if it has value. r = r + p p = '' m = 0 r = r + l if p: # Write the message if loop ended in a pattern. r = r + p p = '' l_string = r # Reset string to modified string. return l_string
The rematcher function seems to do what you want:
EDIT
In your case, the final string should be: