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