Show HN: I wrote a book on Python regular expressions
My book titled "Python re(gex)?" is free to download through this weekend [1][2]
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/
50 comments
[ 2.8 ms ] story [ 118 ms ] threadIs 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.
https://pypi.org/project/regex/
https://github.com/mtrencseni/rxe
So you can write:
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...
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)
My day-to-day experience is in nodejs, where adding one regex object to another coerces them to normal strings first
[edit: hey neat, the hackernews form strips emoji from comments, I wonder if that's just ranges of unicode or if there's some crazy regex going on :D]
There's also a module [1] which already has collection of common regex to match dates, links, emails, etc.
[1] https://github.com/madisonmay/CommonRegex
- 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.
Plus the memoized Packrat algorithm allows throwing in functions with custom conditions. (Somewhat like parser combinators, they also support custom logic.)
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.
Sure, if you dont know what you're doing.
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...