Regex

Python Re Escape

If you’re like me, you’ll regularly sit in front of your code and wonder: how to escape a given character? Challenge: Some characters have a special meaning in Python strings and regular expressions. Say you want to to search for string “(s)” but the regex engine takes the three characters (s) as a matching group. …

Python Re Escape Read More »

Python Regex – How to Count the Number of Matches?

To count a regex pattern multiple times in a given string, use the method len(re.findall(pattern, string)) that returns the number of matching substrings or len([*re.finditer(pattern, text)]) that unpacks all matching substrings into a list and returns the length of it as well. A few hours ago, I wrote a regular expression in Python that matched …

Python Regex – How to Count the Number of Matches? Read More »

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 »