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

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Overall, if possible you want to do a 301 redirect… May 12, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer To get the current timestamp (seconds since 1970), use time()… May 12, 2026 at 4:55 pm
  • Editorial Team
    Editorial Team added an answer I don't think they are Malware/hacking attempts - I have… May 12, 2026 at 4:55 pm

Related Questions

This is a pretty straight forward attempt. I haven't been using python for too
I am using the SharpZipLib open source .net library from www.icsharpcode.net My goal is
I'm sure this is a pretty straight forward question. I'm writing a small windows
This should be pretty straight forward but I can't seem to get my newbie
This seems to be pretty straight forward. I need to send email from some

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.