|
|
|
|
|
by Drup
2245 days ago
|
|
There is a really easy solution to this: write regexes with combinators. star (range 'a' 'z')
Naturally, it's trivial to have a `posix` combinator (or any other syntax, really), which allow you to be compact when things are trivial, explicit when they are not, or even a mix of both. You also don't have to conflate parens for priority and capture anymore, and you can create high level combinators.For instance, this is real code to build a regex parsing URLs (translated in a JS-ish syntax): let cset = chars => compl(set(chars));
let prefix = (head, tail) => char(head) ++ rep(tail);
let scheme = rep(cset("/:?#")) ++ str("://");
let host = rep(cset("/:?#"));
let port = prefix(':', digit);
let path = rep(prefix('/', cset("/?#")));
let query = prefix('?', cset("#"));
let fragment = prefix('#', any);
let url = scheme ++ host ++ opt(port) ++ path ++ opt(query) ++ opt(fragment);
It's less compact, but it's still as fast and it's actually readable/maintainable.
It's all the advantages of Perl's regex, without having to suffer Perl's taste in syntax. |
|