8 ms·
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/
- natpalmer1776 7y agoI 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?
- asicsp 7y agoThere are enough differences in terms of syntax and features across languages that I felt separate books would help the user better than a common one. If you consider Python's built-in 're' module, it doesn't support possessive quantifiers, \K, variable-length lookbehind, set operations, control verbs like SKIP, \p{} unicode character sets and so on. 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.
- nurettin 7y agoIt is not really the regex syntax itself that changes (not too many python specific constructs), but rather, how you apply it. For example, you might use capture groups and re.findall, which will return a list of tuples, or re.search, which will return a match object, both of which have different ways of iterating.
- just_myles 7y agoNone 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.
- caymanjim 7y agoPerl'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.
- Lordarminius 7y ago> 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 ?
- cutler 7y agoRuby's regex engine is Onigmo which is very similar to Perl 5.10. Perl was one of Ruby's main influences, along with Smalltalk and Lisp, whereas Python's BDFL was never a fan of Perl. However, Perl's implementation of regular expressions (PCRE) has been widely adopted hence the similarity you refer to. If you compare with Javascript and PHP you'll find they're all similar.
- redis_mlc 7y agoPerl is a superior language. Besides being the fastest scripting language by far ... 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.
- danso 7y agoThanks 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.
- ErikCorry 7y agoThe engine has support but the language doesn't expose it?
- danso 7y agoI 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/ https://pypi.org/project/regex/
- dhairya 7y agoThis is a great resource. Thank you for taking the time to write this and making it available to us!
- Maro 7y agoYou might like: https://github.com/mtrencseni/rxe 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) )
- just_myles 7y agoI 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)
- wwwhizz 7y agoI would not allow the % sign.
- _eht 7y agoYou should allow “$” on the left; as per the RFC...
- just_myles 7y agoWhy I hate regex :) . You think you know it but, you never really do.
- nerdponx 7y agoThat's about the email RFC, not about regex syntax.
- just_myles 7y agoOk cool.
- nathanbarry 7y agoCongrats on writing and publishing this book!
- AlexanderDhoore 7y agoThere 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.
- gravitas 7y agoI 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?
- madelyn 7y agoCan 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.
- IceDane 7y agoParser combinators are the way to go. I don't use python often enough to know if such a library exists for python, but I would assume so.
- cben 7y agoNo experience with them in python, but look also for PEG grammars. They are significantly simpler than the traditional tower of lexer + ambiguous limited lookahead grammar. Plus the memoized Packrat algorithm allows throwing in functions with custom conditions. (Somewhat like parser combinators, they also support custom logic.)
- IceDane 7y agoI couldn't agree more. I honestly find it rather baffling that someone would write a book on not just regular expressions in general, but one particular variant in one particular language. 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.
- smitshah0014 7y agoThank you very much! This is really helpful.
- spazzy81 7y agoThis is super helpful! I've always felt Python regex was one of the more complicated parts of the language.