Thanks for sharing! I only skimmed it but I liked how you include usage of the external regex module, which I hadn't realized allowed for the use of variable-length look-behinds.
The engine has support but the language doesn't expose it?
Show HN: I wrote a book on Python regular expressions
11–20 of 51 posts
Re: Show HN: I wrote a book on Python regular expressions
#12You might like: 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) )
Re: Show HN: I wrote a book on Python regular expressions
#13You might like: 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) )
Re: Show HN: I wrote a book on Python regular expressions
#14You might like: 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) )
Re: Show HN: I wrote a book on Python regular expressions
#15I personally learned RegEx with PowerShell for a work initiative that required heavy usage of it. Most of the documentation regarding RegEx was pretty language agnostic, so it's interesting when I run across guides that are specific to a particular language. Is there anything about Regular Expressions in Python that creates a unique need for it's own domain specific guide?
Re: Show HN: I wrote a book on Python regular expressions
#16You might like: 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) )
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)
Re: Show HN: I wrote a book on Python regular expressions
#17You might like: 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) )
10 lines to replace maybe 20 characters. How horribly verbose.
Re: Show HN: I wrote a book on Python regular expressions
#18You might like: 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) )
Too verbose, I prefer good old regular expression.
Re: Show HN: I wrote a book on Python regular expressions
#19You might like: 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.Re: Show HN: I wrote a book on Python regular expressions
#20I personally learned RegEx with PowerShell for a work initiative that required heavy usage of it. Most of the documentation regarding RegEx was pretty language agnostic, so it's interesting when I run across guides that are specific to a particular language. Is there anything about Regular Expressions in Python that creates a unique need for it's own domain specific guide?
None that I know of. My thought process has always been that regex was language agnostic. I think Perl has its own version that is widely used. But other than that, I do not know.