Reputation: 321
I'm trying to build a time tracking application which needs to parse a string which can have (but not always) a time in it in one of many formats or combinations. The formats I'm checking for are
These can be with a space in-between the number and the hour or minutes and can be combined or just one or the other. Some examples:
The regex for matching the times that I have currently:
((\d+(\.\d+)?)\s*(h|hr|hrs|hours))?(\s*(\d+)\s*(m|min|mins|minutes))?
This works fine if I just pass it the time string without anything before it. My problem is that I want to parse a full text string with the time appearing anywhere in it. Some examples:
Does anyone have any suggestions on how to tackle this?
Upvotes: 1
Views: 1306
Reputation: 60174
All you really need to do with yours is make the plural s
optional, and add some word boundary tokens.
Try:
\b((\d+(\.\d+)?)\s*(h|hr|hrs?|hours?))?(\s*(\d+)\s*(m|min|mins?|minutes?))?\b
Upvotes: 1