views:

35

answers:

2

I'm writing an application that involves having users enter time's in the following format:

1m30s # 1 Minute,  30 Seconds

3m15s # 3 Minutes, 15 Seconds

2m25s # 2 Minutes, 25 Seconds

2m    # 2 Minutes

55s   # 55 Seconds

The data can have a single "minute designation", a single "second designation", or both. What is the proper way to parse these strings into a format similar to:

{
    "minutes" : 3
    "seconds" : 25
}
+1  A: 

Regex to the rescue!

>>> import re
>>> minsec = re.compile(r'(?P<minutes>\d+)m(?P<seconds>\d+)s')
>>> result = minsec.match('1m30s')        
>>> result.groupdict()
{'seconds': '30', 'minutes': '1'}

Edit: Here is a revised solution:

import re
pattern = r'(?:(?P<minutes>\d+)m)?(?:(?P<seconds>\d+)s)?'

minsec = re.compile(pattern)

def parse(s, pat=minsec):
    return pat.match(s).groupdict()

tests = ['1m30s', '30s', '10m29s']
for t in tests:
    print '---'
    print ' in:', t
    print 'out:', parse(t)

Outputs:

---
 in: 1m30s
out: {'seconds': '30', 'minutes': '1'}
---
 in: 30s
out: {'seconds': '30', 'minutes': None}
---
 in: 10m29s
out: {'seconds': '29', 'minutes': '10'}
jathanism
Nice! I was working on a similar response, but yours is better. I never knew about naming the match groups like that.
Colin
+5  A: 
import re

tests=['1m30s','3m15s','2m25s','2m','55s']
for time_str in tests:
    match=re.match('(?:(\d*)m)?(?:(\d*)s)?',time_str)
    if match:
        minutes = int(match.group(1) or 0)
        seconds = int(match.group(2) or 0)
        print({'minutes':minutes,
               'seconds':seconds})

# {'seconds': 30, 'minutes': 1}
# {'seconds': 15, 'minutes': 3}
# {'seconds': 25, 'minutes': 2}
# {'seconds': 0, 'minutes': 2}
# {'seconds': 55, 'minutes': 0}
unutbu
I'm too slow. You win.
Nathon