RegEx trees and forrest: matching a time.
Posted by jpluimers on 2013/12/17
I was astonished at the number of different ways RegEx can match time.
- regex – Regular expression for matching HH:MM time format – Stack Overflow.
- java – How to extract time from a string using regex? – Stack Overflow.
- c# – Regular expression to validate valid time – Stack Overflow.
- regex – Matching a time string with a regular expression – Stack Overflow.
- c# – Regex match zero or one time a string – Stack Overflow.
- regular expression for time – Stack Overflow.
- expression – Regex pattern for HH:MM:SS time string – Stack Overflow.
Examples like this is why I try to avoid RegEx, as it is totally unclear to me which of the solutions really works and more importantly: why not.
–jeroen






Wouter said
It’s always fun to see so many solutions to the same problem. But in this case, all of them suck :-)
In the first link, somebody on StackOverflow asks how to validate a time formatted as HH:MM with regular expressions.
It’s astonishing how many wrong answers on StackOverflow are plain wrong:
3 out of 5 (!!!).
Now imagine a more complex situation.
It makes sense that bugs creep in. They can be hard to spot in a rx. When I have to modify a regexp months after writing, it’s often faster to start from scratch. You cannot place proper comments in between, and don’t even bother to step through with a debugger. You have to write a lot of unit tests to deal with all sorts of weird situations to make sure that you don’t oversee something.
I use regular expressions to get stuff done quickly. It’s fast to write, but hard to maintain, but it always feels like a hack.
So when something needs to be re-used, or needs to be fast and stable, I try to stay away from them whenever I can. It keeps my code fast, clean, portable, and without dependencies on yet another library.
Where LINQ tried to introduce type-safety to querying, with regular expressions you do the exact opposite by placing your parsing logic in a huge string that gets evaluated at run-time.
jpluimers said
Great write up that exactly words how I feel about this.
I try to minimize RegEx in my code base, and whenever I use it there is this little voice in the back of my head telling me “you are going to regret this”. That voice has been correct too often (:
–jeroen