Stripping time (in the format hh:mm) from a string in Python 2 -


i have been trying strip out occurrences of sequences of text in format

'%d:%d' 

(for example, 05:45) string. have tried following code:

def remove_times(s):     in range(len(s)):         if s[i] == ':':             s = s.replace(s[i-2:i]+':'+s[i:i+3], '') return s 

this not solution problem few reasons , doesn't work in form given above either.

my problem don't know how replace patterns take form above. seems there neat way of doing can't find it.

for example sample input be:

'the time currently: 05:52' 

and corresponding output be:

'the time currently: ' 

so whole time gets deleted input , returned.

i think you're looking re.sub.

import re def remove_times(s):     return re.sub(r'[012]?[0-9]:[0-5][0-9]', '', s) 

result:

>>> remove_times('hi name 4:30 bob') 'hi name  bob' 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -