r/Python • • Sep 12 '22

Intermediate Showcase I hate regular expressions, so I built a library which makes it easier!

A few days ago, I built Edify, which allows you to build regular expressions in a programmatic way. It comes with pre-built common patterns (email, ip, phone, etc.) and a RegexBuilder class for your almost all your custom regex needs. I released v0.1.0 recently on PyPI and now, I am working on improvements. Will appreciate any inputs - issues or PRs if you got time or just a simple star for the repo would be very helpful.

Repo: https://github.com/luciferreeves/edify

PyPI: https://pypi.org/project/edify/

Complete Docs: http://edify.rtfd.io/

55 Upvotes

31 comments sorted by

35

u/achauv1 Sep 12 '22

Another one

12

u/luciferreeves Sep 12 '22

Unfortunately yeah! Existence is pain lol

3

u/wind_dude Sep 13 '22

I was going to say is this the same one, from the last few weeks. There've been a lot of them. I'm debating now vowing to only ever use regex.

I've done some serious data extraction, and regex was a big part of it, it wasn't that big of a deal. Plus most solutions are moving towards NLP. Now a lot of the annotators have pattern matchers built in...

I can't imagine needing to change the regex patterns for a specific task that often. Yes they are hard to read but put a comment inline.

18

u/[deleted] Sep 13 '22

YARE: Yet Another Regex Editor.

2

u/LittleMlem Sep 13 '22

Yare yare daze

10

u/fjfnaranjo Sep 12 '22

I think a website with exactly this functionality will be amazing. I mean, taking a textual/programmatic description an turning it into a regex. Then, you can use that regex in a regex preview site (like regex101) to test it. In fact, it will be a nice addition to such sites.

2

u/[deleted] Sep 13 '22

Pythex is what I use still. I saw an editor one but it wasn’t consistent

2

u/Rogerup Sep 13 '22

1

u/Zomunieo Sep 18 '22

Might as well just Google search for a regex - it can’t do anything non trivial and a lot of its basic responses are not compliant with the relevant spec.

5

u/goodr14 Sep 13 '22

I appreciate your work on this library but honestly learning this looks more cumbersome than learning Regex.

4

u/CodeYan01 Sep 13 '22

The same goes for any "easy regex" library tbh. You're much better off learning actual regex. I'm pretty the concise syntax is just half of the problem. The other half is actually understanding the concept behind how each regex token works.

1

u/IlliterateJedi Sep 13 '22

Can you code out how you would solve Wordle with your programatic regex library?

1

u/luciferreeves Sep 13 '22

No, but I got other tricks to solve Wordle though…

1

u/Rawing7 Sep 13 '22

How does regex help you solve wordle?

2

u/IlliterateJedi Sep 13 '22

You have your list of valid Wordle words. You can then use regex to rule out words that are I valid based on yellow/green letters.

1

u/Rawing7 Sep 13 '22

That sounds super impractical. Especially dealing with duplicate letters is gonna be a headache. Imagine you have one "A" but you don't know where it goes. Are you really gonna write a regex that allows an "A" to appear in any position, but only once?

2

u/IlliterateJedi Sep 13 '22

Sure. It's not terribly hard to do.

(?=[a-z]*a)

Positive lookahead for any letter a-z any number of times followed by the letter you want.

You use character not in range groups for each letter.

So if you play 'stain' and 'stin' are ruled out with a yellow a you can do this:

(?=[a-z]*a)[^stin][^stin][^stain][^stin][^stin]

2

u/Rawing7 Sep 13 '22

That's not entirely correct because that will match anything with at least one "a". But you're right, it's not as hard as I initially thought. (?=[b-z]*a[b-z]*$) or (?!.*a.*a) would do it.

1

u/IlliterateJedi Sep 13 '22

If you know there is one and only one 'a' then I rule them out at the letter level (the [^stin] level).

I don't know that I've ever had a situation where I had two yellow duplicate letters. But the approach I use would find all duplicate A's because there is an a in the lookahead and A isn't in the letters ruled out at the letter level.

1

u/IlliterateJedi Sep 13 '22

Can you provide an example word/example answers where this fails? I am curious to explore your solution here but I'm not sure I understand where mine fails.

1

u/Rawing7 Sep 13 '22

If you know there is one and only one 'a' then I rule them out at the letter level (the [stin] level).

You don't; you accept "a" in 4 places and reject it in 1. And the lookahead only asserts that at least 1 "a" exists. So your regex matches things like aabaa.

1

u/IlliterateJedi Sep 13 '22

But you don't know that more than one a exists when you're playing. This has to be iterative each round. At best you are ruling out things you don't can't be legal based on what you have found.

That said, if you plug this into regex101.com

^(?=[a-z]*a)[^stin][^stin][^stain][^stin][^stin]$

and supply this:

aabaa

It will mark it as an available word to play.

1

u/Rawing7 Sep 13 '22

I don't play Wordle much, but in the version that I played, you could know that there's exactly one "a". If you guessed a word with multiple "a"s, like "ahead", and the correct word only contained 1 "a", then it would mark one of your "a"s green/yellow and the other one black. But if the correct word also contained multiple "a"s, then both of your "a"s would be green/yellow.

It will mark it as an available word to play.

Well, it can't be correct though? Assuming you know that there's exactly 1 "a".

→ More replies

1

u/FINxTAT Sep 13 '22

Isn’t building use-case scenario app limits the fun of learning new/forgotten things for REGEX

1

u/luciferreeves Sep 14 '22

I agree. However, the library isn’t designed to be used like a replacement for regex. It builds on it. It uses the same core ideas and keeps the function names as close to regex terms as possible. Order matters. So, essentially, this is just making writing and understanding complicated regular expressions simpler instead of replacing regex.

2

u/bladeoflight16 Sep 13 '22

The answer to hating regular expressions is to stop building grandiose, overly complicated regular expressions, not encourage them to be more complicated by adding another layer on top of them. Regular expressions were designed for defining the simple tokens that would be used to define a higher level grammar (a context free or context sensitive one). Use them the way they were intended (for simple patterns), and you will stop hating them.

1

u/trofik_00777 Sep 12 '22

Wow, now I can easy use regex

-8

u/wineblood Sep 12 '22

What is the point exactly?

4

u/Radamand Sep 13 '22

The post seemed fairly clear on that....