birdman
Moderator
Ah, the beauty of regular expressions...just add some more bits to it:How on earth to deal with these stupid appended strings! Enough to make you sick!
Code:
[mysys]: cat trimmer.py
#!/usr/bin/python
#
import re
str = "A programme description [internal comment] with flags. (Ep1/7). [HD] [S] Followed by The News [AD,S] Then Reporting Scotland."
trimmer_re = re.compile('\[[^[]*?\](\s*| then[^][]*?| follow[^][]*?)$', flags=re.UNICODE|re.IGNORECASE)
sub_made = True
while sub_made:
print "From:", str
(str, sub_made) = re.subn(trimmer_re, '', str)
print " to:", str
print str
[mysys]:
[mysys]: ./trimmer.py
From: A programme description [internal comment] with flags. (Ep1/7). [HD] [S] Followed by The News [AD,S] Then Reporting Scotland.
to: A programme description [internal comment] with flags. (Ep1/7). [HD] [S] Followed by The News
From: A programme description [internal comment] with flags. (Ep1/7). [HD] [S] Followed by The News
to: A programme description [internal comment] with flags. (Ep1/7). [HD]
From: A programme description [internal comment] with flags. (Ep1/7). [HD]
to: A programme description [internal comment] with flags. (Ep1/7).
From: A programme description [internal comment] with flags. (Ep1/7).
to: A programme description [internal comment] with flags. (Ep1/7).
A programme description [internal comment] with flags. (Ep1/7).
[mysys]: