3 ms·
"Natural language" being .startOfInput .optional.string('0x') .capture .exactly(4).anyOf .range('A', 'F') .range('a', 'f') .range('
by rob74 6y ago
"Natural language" being
.startOfInput
.optional.string('0x')
.capture
.exactly(4).anyOf
.range('A', 'F')
.range('a', 'f')
.range('0', '9')
.end()
.end()
.endOfInput
...ok, that's slightly more readable than a regex, because it "unpacks" the arcane syntax, but you still have to be familiar with the workings of regular expressions to understand it. And it also has the disadvantage that you are replacing the widely understood regex syntax with a niche "DSL" - sort of like using an exotic framework for a popular programming language.
- TeMPOraL 6y agoCommon Lisp had something like this for ages in CL-PPCRE library, with the added benefit of not having to monkey with ".end()" and whatnot, because s-expression syntax guarantees you always know what is enclosed by what. Still, I came to the similar conclusion as you. It's sometimes nice to check a descriptive format if needed (CL-PPCRE can take a "standard" regex and translate it to its tree-based verbose notation), but it's a chore to type that up and it still requires you to understand how the grammar works. It's much easier to work with the standard notation. And I mean - what's the big problem here? Regexes are so common and so useful that, in my opinion, any self-respecting developer should learn them to the point they feel comfortable with reading and writing them, with a manual on hand. If you want to make it easier to read, just split it up and comment it, be it with 'x' modifier or with string concatenation and your programming language's native comments.