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) )
Show HN: I wrote a book on Python regular expressions
21–30 of 51 posts
Re: Show HN: I wrote a book on Python regular expressions
#22Earlier 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.
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
#23You 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
#24You 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
#25- 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
#26There 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
#27There 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.
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
#28I 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.
+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
#29Earlier 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...