tricky regular expressions

Mitja Trampus nun at example.com
Tue Feb 7 16:38:19 EST 2006


[hint: posting the same question in newsgroups generally 
does not help to get responses any quicker]

Ernesto wrote:
 > The string I'm getting data from looks like this:
 > [USELESS DATA]
 > Request : Play
 > [USELESS DATA]
 > Title: Beethoven's 5th
 > [USELESS DATA]
 > Request : next
 > [USELESS DATA]
 > Title:  song #2
 > .....
 > The problem is that I only want the "Titles" which are 
either:
 > a) Followed by "Request : Play"
 > b) Followed by "Request : next"
 >
 > I'm not sure if I should use RE's or some other mechanism.

I'd advise against REs - they can quickly get messy. What 
I'd do is just what you have described:
1) read all lines that are not [USELESS DATA] (i.e. lines 
beginning with either Title or Request) into a list
2) walk through this list, deleting all "Title" lines that 
are not followed by an appropriate "Request" line.

Your description is not very exact - but if anything else 
needs to be filtered out, just do so. The general idea is to 
break your task into smaller steps (instead of one huge RE) 
that are easier to manage, write and understand.



More information about the Python-list mailing list