Live data from Hacker News

Show HN: I wrote a book on Python regular expressions

news.ycombinator.com

21–30 of 51 posts

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

#21
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) )

To me "exactly" means the same as "literal". (I understand here its a full pattern match)

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

#22
post #17
post #14

Earlier quoted context omitted.

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.

This is good point, and without trying to debate anything, regex is something of an art form for some people and I guess as many people are averse to it as there are high school people hating Shakespear.

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

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

#23
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)

I would not allow the % sign.

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

#24
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)

You should allow “$” on the left; as per the RFC...

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

#25
There two kinds of text search/parse problems:

- The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code.

Please don't use regexes in production software. Learn how to write simple parsing code.

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

#26

There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code. Please don't use regexes in production software. Learn how to write simple parsing code.

I need to validate an input string from the user to ensure it's a 32 character hex (I'm validating it's a NAA format 6 WWN) and regex is just the easy, fast and efficient to do this. It's not a complicated regex and is quite readable to anyone with basic regex knowledge, why is this wrong?

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

#27

There two kinds of text search/parse problems: - The really easy ones. A simple string search/split will do and a regex would be overkill. - The really hard ones. You'll need to fully parse this and using a regex will result in fragile/hard to understand code. Please don't use regexes in production software. Learn how to write simple parsing code.

Can you elaborate a bit on this please? I'd be interested in resources on writing better parsers!

Quickly looking at the python standard lib (urlparse, shlex, etc) and Python packages (NLTK Treebank tokenizer), a lot of packages related to slicing, dicing and parsing strings use a mashup of regex and rule based code.

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

#28

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.

> My thought process has always been that regex was language agnostic.

+1

I was surprised to find that regex in python was not much different from the language I use ruby. Would anyone with sufficient knowledge care to eli15 why this is and how it is implemented ?

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

#29
post #24

Earlier quoted context omitted.

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)

You should allow “$” on the left; as per the RFC...

Why I hate regex :) . You think you know it but, you never really do.
Post reply on HN