Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 52871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:58:29+00:00 2026-05-10T16:58:29+00:00

This is a pretty straight forward attempt. I haven’t been using python for too

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T16:58:29+00:00Added an answer on May 10, 2026 at 4:58 pm

    The rematcher function seems to do what you want:

    def rematcher(re_str, iterable):      matcher= re.compile(re_str)     in_match= 0     for item in iterable:         if matcher.match(item):             if in_match == 0:                 yield item             in_match+= 1         else:             if in_match > 1:                 yield '%s repeats %d more times\n' % (re_str, in_match-1)             in_match= 0             yield item     if in_match > 1:         yield '%s repeats %d more times\n' % (re_str, in_match-1)  import sys, re  for line in rematcher('.*Dog.*', sys.stdin):     sys.stdout.write(line) 

    EDIT

    In your case, the final string should be:

    final_string= '\n'.join(rematcher('.*Dog.*', your_initial_string.split('\n'))) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like it should be pretty straight forward, but I'm apparently confused. I
This seems pretty straight forward but I'm not sure what is going wrong. I'm
This seems pretty a straight-forward question, but I could not find a specific answer
this may sound pretty straight forward, but still I want to post this question
Hopefully this is pretty straight forward, just can't work it out. I need to
So, this is pretty straight forward, but I'm not sure how to do it.
Thought this would be pretty straight forward, but my value is remaining the same
I thought this was pretty straight forward but I don't get the same results
This should have been pretty straightforward using the DOM but I seem to be
This seems to be pretty straight forward. I need to send email from some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.