by Maro · 7y ago · 0 comments
https://github.com/mtrencseni/rxe
So you can write:
username = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '%', '+', '-']))
domain = rxe.one_or_more(rxe.set([rxe.alphanumeric(), '.', '-']))
tld = rxe.at_least_at_most(2, 6, rxe.set([rxe.range('a', 'z'), rxe.range('A', 'Z')]))
email = (rxe
.exactly(username)
.literal('@')
.exactly(domain)
.literal('.')
.exactly(tld)
) r'^[\w.%+-]+@[\w.-]+\.[a-zA-Z]{2,6}$'
which does look awful. But there's no reason you can't do username = r'[\w.%+-]+'
domain = r'[\w.-]+'
tld = r'[a-zA-Z]{2,6}'
email = username + r'@' + domain + r'\.' + tld
which is arguably easier to read than the rxe version for someone familiar with regex.My day-to-day experience is in nodejs, where adding one regex object to another coerces them to normal strings first
[edit: hey neat, the hackernews form strips emoji from comments, I wonder if that's just ranges of unicode or if there's some crazy regex going on :D]
There's also a module [1] which already has collection of common regex to match dates, links, emails, etc.
email_reg = re.compile (r'''
([a-zA-Z0-9._%+-]+ #First name and last name
@ #@ sign
[a-zA-Z0-9._%+-]+ #domain name
\.[a-zA-Z]{2,10}) #.com
''',re.VERBOSE)
But to give a bit of substance, you can often use a dictionary type approach in a situation where regex is needed. Example: replacing accented latin with normal (ascii) latin.
I do sometimes pride myself in necromancing skills of resurrection old Perl scripts from perlmonks.com but I suspect it is more of a hobby that out of absolute necessity. I find the memes about Perl/Python and Starwars to be pretty funny and much more entertaining than people actually debating programming languages. [1]
[1] https://www.python.org/doc/humor/#python-vs-perl-according-t...