3 ms·
A regular expression is a domain-specific language that defines a language consisting of all the strings it matches. In principle, this serves as executable do
by braythwayt 7y ago
A regular expression is a domain-specific language that defines a language consisting of all the strings it matches.
In principle, this serves as executable documentation.
A regular expression that matches valid regular expressions could be viewed as a human-readable documentation of valid regular expression syntax that has the valuable side-effect of being executable.
If, instead, we detect whether a string is a regular expression by compiling it, we may find it very difficult to read the compiler source code.
It is much more work to make an optimized regular expression engine that is also self-documenting than a regular expression.
- ChrisSD 7y agoI think we have different definitions of human readable documentation. I'd find normal code, with or without appropriate comments, far more readable than a mess of regex symbols.
- braythwayt 7y agoRegular expressions are not my favourite either, but one thing I will say is that normal code becomes difficult to read over time as features are bolted on and optimizations are added to address performance concerns. Whereas, some form of executable DSL can serve as self-testing documentation indefinitely. DSLs can provide a separation of concerns, where the syntax is designed for readability, and the engine is designed for performance. Modern Regexen are a dumpster fire, of course, but you asked why someone might want to, not whether this is something Everyone might want to do.