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

View all comments

Show parent comment

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".

1

u/IlliterateJedi Sep 13 '22

Ah, I see what you're saying. Yeah, it's not super precise on the double letters. As you show, this does narrow it down with double letters:

(?=[a-z]*a[a-z]*a)