Python Regex Syntax [2-Minute Primer]

A regular expression is a decades-old concept in computer science. Invented in the 1950s by famous mathematician Stephen Cole Kleene, the decades of evolution brought a huge variety of operations. Collecting all operations and writing up a comprehensive list would result in a very thick and unreadable book by itself. Fortunately, you don’t have to learn all regular expressions before you can start using them in your practical code projects. Next, you’ll get a quick and dirty overview of the most important regex operations and how to use them in…

Read More
java 

Python Regex Superpower [Full Tutorial]

What’s the best-kept productivity secret of code masters? Here’s what ex-Google’s tech lead says is the most important skill as a coder (spoiler: it has to do with the topic of the tutorial): Congratulations – you’re about to become a regular expression master. I’ve not only written the most comprehensive free regular expression tutorial on the web (16812 words) but also added a lot of tutorial videos wherever I saw fit. So take your cup of coffee, scroll through the tutorial, and enjoy your brain cells getting active! If you…

Read More

Python Regex Methods – A Short Overview

Python’s re module comes with a number of regular expression methods that help you achieve more with less. Think of those methods as the framework connecting regular expressions with the Python programming language. Every programming language comes with its own way of handling regular expressions. For example, the Perl programming language has many built-in mechanisms for regular expressions—you don’t need to import a regular expression library—while the Java programming language provides regular expressions only within a library. This is also the approach of Python.  These are the most important regular…

Read More
Programming 

Python Regex And Operator [Tutorial + Video]

This tutorial is all about the AND operator of Python’s re library. You may ask: what? (And rightly so.) Sure, there’s the OR operator (example: ‘iPhone|iPad’). But what’s the meaning of matching one regular expression AND another? There are different interpretations for the AND operator in a regular expression (regex): Ordered: Match one regex pattern after another. In other words, you first match pattern A AND then you match pattern B. Here the answer is simple: you use the pattern AB to match both. Unordered: Match multiple patterns in a string…

Read More
Programming 

Python Regex Or – A Simple Illustrated Guide

This tutorial is all about the or | operator of Python’s re library. You can also play the tutorial video while you read: What’s the Python Regex Or | Operator? Given a string. Say, your goal is to find all substrings that match either the string ‘iPhone’ or the string ‘iPad’. How can you achieve this? The easiest way to achieve this is the Python or operator | using the regular expression pattern (iPhone|iPad). Here’s an example: >>> import re >>> text = ‘Buy now: iPhone only $399 with free iPad’…

Read More
Programming 

Python Regex – How to Match the Start of Line (^) and End of Line ($)

This article is all about the start of line ^ and end of line $ regular expressions in Python’s re library. These two regexes are fundamental to all regular expressions—even outside the Python world. So invest 5 minutes now and master them once and for all! Python Re Start-of-String (^) Regex You can use the caret operator ^ to match the beginning of the string. For example, this is useful if you want to ensure that a pattern appears at the beginning of a string. Here’s an example: >>> import re…

Read More

Python Regex Sub

Do you want to replace all occurrences of a pattern in a string? You’re in the right place! This article is all about the re.sub(pattern, string) method of Python’s re library. Let’s answer the following question: How Does re.sub() Work in Python? The re.sub(pattern, repl, string, count=0, flags=0) method returns a new string where all occurrences of the pattern in the old string are replaced by repl. Here’s a minimal example: >>> import re >>> text = ‘C++ is the best language. C++ rocks!’ >>> re.sub(‘C\+\+’, ‘Python’, text) ‘Python is the…

Read More

Python Regex Superpower [Ultimate Guide]

This tutorial is a chapter excerpt drafted for my new book “Python One-Liners” (to appear in 2020, No Starch Press, San Francisco). Are you an office worker, student, software developer, manager, blogger, researcher, author, copywriter, teacher, or self-employed freelancer? Most likely, you are spending many hours in front of your computer, day after day. In any case, improving your daily productivity—only by a small fraction of a percentage—will amount for thousands, if not tens of thousands of dollars of productivity gain. And more importantly, if you are not merely clocking…

Read More