Matching a string up to a word
bokr at oz.net
Thu Sep 5 00:51:44 EDT 2002
On Wed, 04 Sep 2002 22:31:46 -0400, Peter Hansen <peter at engcorp.com> wrote:
>> In article <3d75ffaa$1 at news.sentex.net>, Peter Hansen wrote:
>>>Can you instead guarantee the format of the date that follows the
>>>filename? Will it always consist of the day of the week, the month,
>>>the date, and the year? That would turn this into a relatively
>> Yes I can guarentee the last four fields be in that date format.
>> Actually I think that solved it. I'll split on whitespaces into
>> an array, and remove the first and last four fields, and then join
>> whatever's left. That should do it. I was stuck thinking how to
>> parse it left to right.
>I was thinking of something like that. This should do it then:
> filename = ' '.join(field.split(' ')[1:-4])
>That is, provided you never expect to see more than one consecutive
>space in your filenames...
Why should that hurt, if the head and tail items are consistently separated
by single spaces?
>>> line = '12345 /some weird/path with multiple- -spaces Wed Sep 4 2002.'
>>> line.split(' ')
['12345', '/some', '', 'weird/path', 'with', 'multiple-', '', '', '', '-spaces', 'Wed', 'Sep', '4', '2002.']
>>> line.split(' ')[1:-4]
['/some', '', 'weird/path', 'with', 'multiple-', '', '', '', '-spaces']
>>> ' '.join(line.split(' ')[1:-4])
'/some weird/path with multiple- -spaces'
More information about the Python-list