Parsing multiple lines from text file using regex

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Oct 27 20:40:53 EDT 2013


On 28/10/2013 00:35, Marc wrote:
>> What was wrong with the answer Peter Otten gave you earlier today on the
>> tutor mailing list?
>>
>> --
>> Python is the second best programming language in the world.
>> But the best has yet to be invented.  Christian Tismer
>>
>> Mark Lawrence
>>
>
>
> I did not receive any answers from the Tutor list, so I thought I'd ask
> here.  If an answer was posted to the Tutor list, it never made it to my
> inbox.  Thanks to all that responded.
>

Okay, the following is taken directly from Peter's reply to you.  Please 
don't shoot the messenger :)

You can reference a group in the regex with \N, e. g.:

 >>> text = """"banner option delim
... banner text
... banner text
... banner text
... delim
... """
 >>> re.compile(r"banner\s+(\w+)\s+(\S+)\s+(.+?)\2", re.MULTILINE |
re.DOTALL).findall(text)
[('option', 'delim', 'banner text\nbanner text\nbanner text\n')]


-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence




More information about the Python-list mailing list