tags:

views:

52

answers:

3

I need a regular expression that detects timer strings that could be less than or greater than a minute. So the expression would need to match both:

35.54 and 1:03.24

+1  A: 

([0-9]+:|)[0-5][0-9]\.[0-9][0-9] ought to do it.

Will A
Out of curiosity, why "[0-9]+[0-9]+"? Couldn't that just be "[0-9]+"? Also, that period should probably be escaped with a slash or it'll match anything, not just a period.
Faisal
That would also pick up 1:99.99 (or one or more minutes and any seconds that are actually greater than a minute).
npsken
True - but 99:99.99 is a legit timing, honest. :)
Will A
@Faisal - I'd not posted for more than ten seconds before I removed the [0-9]+ - you caught me quickly on that one - have escaped the . - good spot.
Will A
@Will A: I should probably rest my itchy trigger finger and let people edit their answers...I know I always end up having to, too. :)
Faisal
@KPthunder that was the goal... anything from 1 second to 59 minutes. this solution worked well for me.
Greg
+1  A: 
(\d+:)?(\d{2})\.(\d{2})

1 or more digits, and a colon (optional)
2 digits
dot
2 digits

For each matched group, ensure that the numbers make sense. Embedding 0-60 in regex is just silly.

matching = regex.text( str );
matching |= $2 < 60;
matching |= $3 < 60;
Stefan Kendall
A: 
(\d?\d?):?(\d\d)\.(\d\d)

This will match both types of strings and allows the time elements to be referenced as 3 separate capturing groups

Gary