The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,860 other subscribers

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.

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

2 Responses to “RegEx trees and forrest: matching a time.”

  1. Wouter's avatar

    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's avatar

      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

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.