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

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -