Stephan Houben writes:

 > FWIW, I also strongly prefer the Verbal Expression style and consider
 > "normal" regular expressions to become quickly unreadable and
 > unmaintainable.
 > 
 > Verbal Expressions are also much more composable.

So are grammars.

But REs aren't so bad or incomposable if you build them up slowly in a
grammar-like fashion and with a specific convention for groups:

    atom = r"[-%A-Za-z0-9]+"   # incorrect, for example only
                               # each component has different lexical
                               # restrictions
    scheme = user = password = rf"({atom})"
    domain = rf"((?:{atom}\.)+{atom})"
    port = r"([0-9]+)"
    authority = rf"(?:{user}(?::{password})?@)?{domain}(?::{port})?"
    path = rf"((?:/{atom})+/?)"

    # Incorrect, but handles many common URIs.
    url = rf"{scheme}://(?:{authority})?({path})"

Of course this is parsing with regular expressions, which is generally
frowned upon, and it would be even uglier without f-strings.  The
non-capturing groups admittedly are a significant distraction when
reading.  It's about the limit of what I would do if I didn't have a
parsing library but did have REs (more complex than this and I'd write
my own parser).

I will concede that it took me 15 minutes to write that, of which 4
were spent testing and fixing one bug (which was a real bug; there
were no syntax errors in the REs).  Some of the time was spent
deciding how closely to follow the RFC 3986 generic syntax, though.  I
will also concede that I've been writing REs since 1981, although not
as frequently in the last 15 years as in the first 20.

Would you like to write that using VEs and show us the result?  Don't
forget to document the indicies for extracting the scheme, user,
password, domain, port, and path (in my RE, they are 1-6).

Steve

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to