Reputation:
I want to split time from decription strings.
desc
can have this kind of strings:
desc = '08:59 Hudh aes ....'
desc = '19:59Aksi jeh....'
desc = 'just letters without time'
desc = '21.19:55Aksi hsue....'
desc = '256.08:59Aksi mane....'
time
contains 10 first letters of description
I want to find :
and then to take two numbers before it and after it, so i can split the time
time = ''.join(filter(lambda x: x, desc[0:10].replace(" " , "")))
print "Time:" , time , time.rsplit(':', 1)[0]
time.rsplit(':', 1)[0]
returns all numbers before :
time.rsplit(':', 1)[0]
returns all numbers after :
How to define to split only two numbers after and before :
?Is this a good way? Is better idea to use regex, i tried but it's a little complicated?
Upvotes: 1
Views: 666
Reputation: 6272
If the strings are separated each other you can use a re.search
method like this:
import re
[hour, min, desc] = re.search(r'(?:(?:\d+\.)?(\d{2}):(\d{2}))?[ \t]*(.*)',desc).groups()
# --
# desc = '08:59 Hudh aes ....'
# hour -> '08'
# min -> '59'
# desc -> 'Hudh aes ....', the space before the description are stripped too
# --
# desc = '19:59Aksi jeh....'
# hour -> '19'
# min -> '59'
# desc -> 'Aksi jeh....'
# --
# desc = 'just letters without time'
# hour -> None
# min -> None
# desc -> 'just letters without time'
# --
# desc = '256.08:59Aksi mane....'
# hour -> '08'
# min -> '59'
# desc -> 'Aksi mane....'
Upvotes: 0
Reputation: 774
You can try re.finditer()
test = """desc = '08:59 Hudh aes ....'
desc = '19:59Aksi jeh....'
desc = 'just letters without time'
desc = '21.19:55Aksi hsue....'
desc = '256.08:59Aksi mane....'
"""
pattern = r"\d{2}:\d{2}"
for m in re.finditer(pattern,test):
print m.group()
Output will be:
08:59
19:59
19:55
08:59
Then from this output you can split the hours and minutes easily
Upvotes: 1
Reputation: 87064
How to define to split only two numbers after and before :
With regex; the following regex pattern matches exactly 2 digits, followed by a colon, followed by exactly two digits (which is literally what you ask for):
import re
desc = '21.19:55Aksi hsue....'
m = re.search(r'(\d{2}):(\d{2})', desc)
if m:
hour, min = m.groups()
else:
# no time in desc string
pass
print 'hour = {}, min = {}'.format(hour, min)
Output
hour = 19, min = 55
Upvotes: 1