Regex

Python re.findall() – Everything You Need to Know

When I first learned about regular expressions, I didn’t really appreciate their power. But there’s a reason regular expressions have survived seven decades of technological disruption: coders who understand regular expressions have a massive advantage when working with textual data. They can write in a single line of code what takes others dozens! This article …

Python re.findall() – Everything You Need to Know Read More »

Python Regex Match

Why have regular expressions survived seven decades of technological disruption? Because coders who understand regular expressions have a massive advantage when working with textual data. They can write in a single line of code what takes others dozens! This article is all about the re.match() method of Python’sΒ re library. There are two similar methods to …

Python Regex Match Read More »

Python Regex Fullmatch

Why have regular expressions survived seven decades of technological disruption? Because coders who understand regular expressions have a massive advantage when working with textual data. They can write in a single line of code what takes others dozens! This article is all about the re.fullmatch(pattern, string) method of Python’s re library. There are three similar methods …

Python Regex Fullmatch Read More »

Python Regex Compile

The method re.compile(pattern) returns a regular expression object from the pattern that provides basic regex methods such as pattern.search(string), pattern.match(string), and pattern.findall(string). The explicit two-step approach of (1) compiling and (2) searching the pattern is more efficient than calling, say, search(pattern, string) at once, if you match the same pattern multiple times because it avoids …

Python Regex Compile Read More »

Python Regex Flags

In many Python regex functions, you see a third argument flags. What are they and how do they work? Flags allow you to control the regular expression engine. Because regular expressions are so powerful, they are a useful way of switching on and off certain features (e.g. whether to ignore capitalization when matching your regex). …

Python Regex Flags Read More »

Python Regex Split

I’m always surprised how regular expressions survived seven decades of technological disruption. They look much the same as 70 years ago. This means that if you master regular expressions, you build yourself a lasting and highly relevant skill in today’s marketplace. You’ll be able to write in a single line of code what takes others …

Python Regex Split Read More »

Python Regex Sub

Do you want to replace all occurrences of a pattern in a string? You’re in the right place! The regex function re.sub(P, R, S) replaces all occurrences of the pattern P with the replacement R in string S. It returns a new string. For example, if you call re.sub(‘a’, ‘b’, ‘aabb’), the result will be …

Python Regex Sub Read More »

Python Regex Search

When I first learned about regular expressions, I didn’t appreciate their power. But there’s a reason regular expressions have survived seven decades of technological disruption: coders who understand regular expressions have a massive advantage when working with textual data. They can write in a single line of code what takes others dozens! This article is …

Python Regex Search Read More »

Python Re Dot

You’re about to learn one of the most frequently used regex operators: the dot regex . in Python’s re library. You can also watch the walk-through video as you read through the tutorial: Related article: Python Regex Superpower – The Ultimate Guide What’s the Dot Regex in Python’s Re Library? The dot regex . matches …

Python Re Dot Read More »

Python Re * – The Asterisk Quantifier for Regular Expressions

Every computer scientist knows the asterisk quantifier of regular expressions. But many non-techies know it, too. Each time you search for a text file *.txt on your computer, you use the asterisk operator. But how does it work? This article is all about the asterisk * quantifier in Python’s re library. Study it carefully and master …

Python Re * – The Asterisk Quantifier for Regular Expressions Read More »

56 Python One-Liners to Impress Your Friends

This is a running document in which I’ll answer all questions regarding the single line of Python code. It’s based on my interactive collection here but without the slow videos and embedded code shells. Let’s get started! Python One Line If Else You can use a simple if statement in a single line of code. …

56 Python One-Liners to Impress Your Friends Read More »