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

  • Home
  • SEARCH
  • 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 494205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:28:32+00:00 2026-05-13T05:28:32+00:00

I need to parse text lists: 1 List name 1 item 2 item 3

  • 0

I need to parse text lists:

1 List name

1 item
2 item
3 item

2 List name

1 item
2 item
3 item

3 List name

1 item
2 item
3 item

I was trying to use regular expression to split first level list:

import re
def re_show(pat, s):
 print re.compile(pat, re.S).sub("{\g<0>}", s),'\n'

s = '''
1 List name

1 item
2 item
3 item

2 List name

1 item
2 item
3 item

3 List name

1 item
2 item
3 item
'''

re_show('\n\d+.*?(?=\n\n\d+.*?\n\n)', s)

But it doesn’t work. Instead of this:

{
1 List name

1 item
2 item
3 item}
{
2 List name

1 item
2 item
3 item}
{
3 List name

1 item
2 item
3 item}

I’ve got this:

{
1 List name}
{
1 item
2 item
3 item}
{
2 List name}
{
1 item
2 item
3 item}

3 List name

1 item
2 item
3 item

What would you recommend to solve this task?

Thanks for your answers. I’ve learned many new features of Python.

I think, I will use “state machine” approach as described here

  • 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-13T05:28:33+00:00Added an answer on May 13, 2026 at 5:28 am
    class ListParser:
    
     def __init__(self, s):
      self.str = s.split("\n")
      print self.str
      self.answer = []
    
     def parse(self):
      self.nextLine()
      self.topList()
      return
    
     def topList(self):
      while(len(self.str) > 0):
       self.topListItem()
    
     def topListItem(self):
      l = self.nextLine()
      print "TOP: " + l
      l = self.nextLine()
      if l != '':
       raise Exception("expected blank line but found '%s'" % l)
      sub = self.sublist()
    
     def nextLine(self):
      return self.str.pop(0)
    
     def sublist(self):
      while True:
       l = self.nextLine()
       if l == '':
        return # end of sublist marked by blank line
       else:
        print "SUB: " + l
    
    parser = ListParser(s)
    parser.parse() 
    print "done"
    

    prints

    TOP: 1 List name
    SUB: 1 item
    SUB: 2 item
    SUB: 3 item
    TOP: 2 List name
    SUB: 1 item
    SUB: 2 item
    SUB: 3 item
    TOP: 3 List name
    SUB: 1 item
    SUB: 2 item
    SUB: 3 item
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression to parse a text The directory is /home/foo/bar/hello.txt. I
I need a regular expression to parse a body of text. Basically assume this
I'm trying to parse a text document containing region names and a list of
I need to parse some text in a UITextField and turn it into a
I need the Perl regex to parse plain text input and convert all links
I wanted to parse a text file that contains unstructured text. I need to
I need to parse some text file, create objects for various entities encountered in
I have multiple text files that represent logging entries which I need to parse
I need to parse Windows text file and extract all data related to operations.
I am newbie to Perl. I need to parse a tab separated text file.

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.