Live data from Hacker News

Show HN: I wrote a book on Python regular expressions

news.ycombinator.com

11–20 of 51 posts

Re: Show HN: I wrote a book on Python regular expressions

#11
post #3

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?

I don't think I understand your question (nor am I an expert on Python regex!)...but just to be clear, Python's regular expression standard library is named `re`. But there is an external lib – ostensibly a drop-in replacement – that goes by the name of `regex`. It is the `regex` library that supports variable lookbehind, not Python's standard library `re`

https://pypi.org/project/regex/

Re: Show HN: I wrote a book on Python regular expressions

#12
post #7

You 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

#13
post #7

You 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) )

This is excellent, I prefer this.

Re: Show HN: I wrote a book on Python regular expressions

#14
post #7

You 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

#15

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

Re: Show HN: I wrote a book on Python regular expressions

#16
post #7

You 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) )

I use the below to extract email addresses. Took from some website some time ago and made super light changes to it. Not sure if this is the best but it has served me fairly well.

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

#17
post #14
post #7

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

But that is exactly what I like about it. I rarely have to use regex, but when I do have to write or change something I always have to spend 15 mins reading (remembering) most of the things about it again, this would help me understand my old code way better.

Re: Show HN: I wrote a book on Python regular expressions

#18
post #7

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

Completely agree. If you know good old regular expressions, then this rxe definition is actually harder to read. This is how APL programmers must feel when they look at code in popular programming languages.

Re: Show HN: I wrote a book on Python regular expressions

#19
post #7

You 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) )

This seems cool, but I think that's mostly because the regex you think of as a point of comparison is something like

    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

#20

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

Perl's is a superset ("PCRE" - Perl Compatible Regular Expression). It's now supported by a lot of other tools, not least of which is GNU grep.
Post reply on HN