Stop writing regular expressions. Use verbal expressions

    Immediately make a reservation, if you are a professional in using regular expressions, then you better not read further to avoid ^ (. *) $

    GitHub user jehna has found an interesting method of avoiding the implementation of complex regular expressions in the traditional way.
    Verbal expressions turn a complex and sometimes non-trivial path from logic to a regular expression itself into an unobtrusive walk using an associated set of functions.

    The result, to say the least, is amazing. Here is the verbal expression from the repository's README file for testing URL validity:
    var tester = VerEx()
                .startOfLine()
                .then( "http" )
                .maybe( "s" )
                .then( "://" )
                .maybe( "www." )
                .anythingBut( " " )
                .endOfLine();
    


    Such a style may seem unnecessarily simple to old-school regulars, but even they may agree that in this way it is much easier to describe a simple (for now, approx. Per.) Regular expression.

    The basis of the project is the JS library, but there are also a bunch of implementations in other languages.

    Original article.

    PS If someone is already using this library, please give an example of a complex regular routine written in this style.

    Also popular now: