u/Soggy-Usual-4898

I wrote a RegEx alternative that's actually readable, please share your thoughts
▲ 12 r/regex+1 crossposts

I wrote a RegEx alternative that's actually readable, please share your thoughts

Hey everyone, I'd like to share an open source project I've been working on that I think you may find it useful for your projects: enhex (Enhanced Expression) – a human-readable language for writing regular expressions. This isn't a new pattern-matching system; it adds a readable layer over RegEx patterns to keep them descriptive and maintainable.

Here is an example of the difference in a complex URL pattern:

^https?:\/\/(?:[a-zA-Z\d-]+\.)+[a-z]{2,10}(?::\d{2,5})?(?:\/[^\s\?#]*)?(?:\?[^\s#]*)?(?:#[^\s]*)?$

(Please tell me, can you actually "read" above pattern?)

Instead, you can write this in you code or a .enhex file:

start
+ "http" + optional("s") + "://"
+ one_or_more(
    non_capturing(one_or_more(letter | digit | dash)
    + ".")
)
+ tld() # Top Level Domain (EnhEx internal preset)
+ optional(
    non_capturing(":" + between(2, 5, digit))
)
+ optional(
    non_capturing("/" + zero_or_more(not(whitespace | "?" | "#")))
)
+ optional(
    non_capturing("?" + zero_or_more(not(whitespace | "#")))
)
+ optional(
    non_capturing("#" + zero_or_more(not(whitespace)))
)
+ end

Its GitHub repo is available here for complete information: https://github.com/mkh-user/enhex

It's available in Rust (Crates.io), Python (PyPI), and JS (npm) with the same behavior (Rust is core).

I'm currently working on a VSCode extension for highlighting, autocomplete, and live preview. Do you have any ideas to share?

u/Soggy-Usual-4898 — 9 days ago