193 points · by asicsp · 7y ago · 50 comments
The book covers both 're' and 'regex' modules, has plenty of examples and chapters also have cheatsheets and exercises.
Code snippets, exercises, sample chapters, etc are available on GitHub repo [3]
I used pandoc+xelatex [4] to generate the pdf.
[1] https://gumroad.com/l/py_regex
[2] https://leanpub.com/py_regex
[3] https://github.com/learnbyexample/py_regular_expressions
[4] https://learnbyexample.github.io/tutorial/ebook-generation/customizing-pandoc/
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.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...
Is there anything about Regular Expressions in Python that creates a unique need for it's own domain specific guide?
Another angle is with regards to usage of functions. When to use 're.findall' vs 're.finditer'. How does capture group affect 're.split' and 're.findall' and so on.
+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 ?
Ruby was inspired by it.
Python stil can't do forward references, amongst many other puzzling limitations. I'm sure Python 3's split() improvements were copied from Perl.
- 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've been writing code for over 15 years and if I said I had reached for regular expressions maybe 10 times in that entire time, I'm pretty sure I wouldn't be off even by an order of magnitude.
There are very few cases where they're the appropriate tool, I think. They don't compose(unless you use a higher level library that lets you construct them declaratively maybe), they are hard to read and hard to debug and nearly impossible to extend.
In my opinion, parser combinators are state of the art for building parsers. They can read like prose, are very easy to extend and usually easy to debug as well since you can easily get detailed error messages.
Regex can indeed lead to issues [1] but so could any other piece of code. So, I disagree that regex shouldn't be used in production. This article [2] by Jeff Atwood gives a balanced view of when to use regex and some nice tips.
[1] https://new.blog.cloudflare.com/details-of-the-cloudflare-ou...
[2] https://blog.codinghorror.com/regular-expressions-now-you-ha...
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.
Plus the memoized Packrat algorithm allows throwing in functions with custom conditions. (Somewhat like parser combinators, they also support custom logic.)
Sure, if you dont know what you're doing.