[ANN] ReadableRegex.jl

Hi everyone,

As it randomly came up on Slack today, I might as well announce a small new package here: ReadableRegex.jl GitHub - jkrumbiegel/ReadableRegex.jl: regexes for people who don't really want to learn or read regexes

The point of it is to be able to construct regular expressions in a verbose but easy to understand way, as the functions read almost like a natural sentence. This pays off over time as complex expressions stay easy to comprehend for people reading the source code. Normally, sufficiently complex regexes tend to be treated a bit like “magic” and nobody wants to touch them again if they seem to work.

Here’s an example from the readme. Can you quickly determine, what this regex does?

regex = r"[\+-]?(?:\d*\.)?\d+"

Now try this, without knowing the ReadableRegex syntax:

regex = maybe(["-", "+"]) * maybe(one_or_more(DIGIT) * ".") * at_least_one(DIGIT)

They do the same thing of course, which is matching different floating point numbers. Now imagine adding support for exponential syntax to the first one, for the second one it wouldn’t be that hard. It’s just difficult for humans to parse a sequential string representing nested expressions, obscured through abbreviations and lots of backslashes.

You can install it via ]add ReadableRegex. All docs are in the readme file.

46 Likes

THANKS THANKS!

If there is one thing in my life I will NEVER understand, it is Regex. It is probably more complicated than quantum mechanics!!!
Any time I had to do a regex match, I had to spend hours on stackoverflow, tutorials, and cryptic syntax, and trial and error to get it right.

This simplifies things tremendously!!!

6 Likes

This looks nice! By the way, there is a similar package by @bramtayl, https://github.com/bramtayl/RegularExpressions.jl.

One thing that’s convenient about the macro is that the regex is compiled at macro expansion time, so you can use it in loops without loss of performance (whereas for a function-based approach, I think you would want to create the regex object before the loop and pass it in for maximal performance). I wonder if there’s a nice way to add that feature here.

5 Likes

Ha interesting, the idea seems very similar but still the syntax is different. I do have a @compile macro included by the way, which allows to compile the whole function expression if the parts are known at compile time. It’s at the end of the readme

3 Likes

Man I need to remember this package next time I try to regex. I’m horrible at that stuff.

Ah cool! I had missed that.

@jules I had similar goals when I wrote RegularExpressions.jl. Happy to collaborate if you want to combine efforts

3 Likes

Yes that would be nice, I haven’t used it that much in the wild, yet, so I don’t know which parts could be improved the most. I had a look at your package and it seems to offer a little bit more functionality that I could still add, like recursion and flags

2 Likes

Who do you think you are? Some kind of superhero?

No but seriously, this is an awesome package idea. Thank you for the contribution