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 766695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T17:05:24+00:00 2026-05-14T17:05:24+00:00

A periodic computer generated message (simplified): Hello user123, – (604)7080900 – 152 – minutes

  • 0

A periodic computer generated message (simplified):

Hello user123,

- (604)7080900
- 152
- minutes

Regards

Using python, how can I extract “(604)7080900”, “152”, “minutes” (i.e. any text following a leading "- " pattern) between the two empty lines (empty line is the \n\n after “Hello user123” and the \n\n before “Regards”). Even better if the result string list are stored in an array. Thanks!

edit: the number of lines between two blank lines are not fixed.

2nd edit:

e.g.

hello

- x1
- x2
- x3

- x4

- x6
morning
- x7

world

x1 x2 x3 are good, as all lines are surrounded by 2 empty lines, x4 is also good for the same reason. x6 is not good because no blank line follows it, x7 is not good as no blank in front of it. x2 is good (not like x6, x7) because the line ahead is a good line and the line following it is also good.

this conditions might be not clear when I posted the question:

a continuous of good lines between 2 empty lines

good line must have leading "- "
good line must follow an empty line or follow another good line
good line must be followed by an empty line or followed by another good line

thanks

  • 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. Editorial Team
    Editorial Team
    2026-05-14T17:05:25+00:00Added an answer on May 14, 2026 at 5:05 pm

    The simplest approach is to go over these lines (assuming you have a list of lines, or a file, or split the string into a list of lines) until you see a line that’s just '\n', then check that each line starts with '- ' (using the startswith string method) and slicing it off, storing the result, until you find another empty line. For example:

    # if you have a single string, split it into lines.
    L = s.splitlines()
    # if you (now) have a list of lines, grab an iterator so we can continue
    # iteration where it left off.
    it = iter(L)
    # Alternatively, if you have a file, just use that directly.
    it = open(....)
    
    # Find the first empty line:
    for line in it:
        # Treat lines of just whitespace as empty lines too. If you don't want
        # that, do 'if line == ""'.
        if not line.strip():
            break
    # Now starts data.
    for line in it:
        if not line.rstrip():
            # End of data.
            break
        if line.startswith('- '):
            data.append(line[:2].rstrip())
        else:
            # misformed data?
            raise ValueError, "misformed line %r" % (line,)
    

    Edited: Since you elaborate on what you want to do, here’s an updated version of the loops. It no longer loops twice, but instead collects data until it encounters a ‘bad’ line, and either saves or discards the collected lines when it encounters a block separator. It doesn’t need an explicit iterator, because it doesn’t restart iteration, so you can just pass it a list (or any iterable) of lines:

    def getblocks(L):
        # The list of good blocks (as lists of lines.) You can also make this
        # a flat list if you prefer.
        data = []
        # The list of good lines encountered in the current block
        # (but the block may still become bad.)
        block = []
        # Whether the current block is bad.
        bad = 1
        for line in L:
            # Not in a 'good' block, and encountering the block separator.
            if bad and not line.rstrip():
                bad = 0
                block = []
                continue
            # In a 'good' block and encountering the block separator.
            if not bad and not line.rstrip():
                # Save 'good' data. Or, if you want a flat list of lines,
                # use 'extend' instead of 'append' (also below.)
                data.append(block)
                block = []
                continue
            if not bad and line.startswith('- '):
                # A good line in a 'good' (not 'bad' yet) block; save the line,
                # minus
                # '- ' prefix and trailing whitespace.
                block.append(line[2:].rstrip())
                continue
            else:
                # A 'bad' line, invalidating the current block.
                bad = 1
        # Don't forget to handle the last block, if it's good
        # (and if you want to handle the last block.)
        if not bad and block:
            data.append(block)
        return data
    

    And here it is in action:

    >>> L = """hello
    ...
    ... - x1
    ... - x2
    ... - x3
    ...
    ... - x4
    ...
    ... - x6
    ... morning
    ... - x7
    ...
    ... world""".splitlines()
    >>> print getblocks(L)
    [['x1', 'x2', 'x3'], ['x4']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a periodic task that needs to execute once a minute (using delayed_job).
I am forced to execute a periodic task using a timer that is invoked
I'm using java.util.Timer to schedule a periodic task. At one point, I'd like to
Is there a way using Core Plot to have periodic labels assigned to data
I am using the ScheduledThreadPoolExecutor to execute periodic tasks. It is essential that the
I am using .NET remoting to retrieve periodic status updates from a Windows service
I'm trying create a simple background periodic task using Django-Celery-RabbitMQ combination. I installed Django
I'm using QThread to do some periodic background work, the source code looks something
I have a SQL table with periodic measurements. I'd like to be able to
I am trying to use periodic refresh(ajax)/polling on my site by XMLHttp(XHR) to check

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.