Regex

How to Find Multiple Occurrences of a String Within a String in Python?

Problem Formulation: Given a longer string and a shorter string. How to find all occurrences of the shorter string in the longer one? Consider the following example: Longer string: ‘Finxters learn Python with Finxter’ Shorter string: ‘Finxter’ Result: [‘Finxter’, ‘Finxter’] Optionally, you may also want to print the positions where the shorter string arise in …

How to Find Multiple Occurrences of a String Within a String in Python? Read More »

How to Split a String Between Numbers and Letters?

Problem Formulation: Given a string of letters and numbers. How to split the string into substrings of either letters or numbers by using the boundary between a letter and a number and vice versa. Examples: Have a look at the following examples of what you want to accomplish. ‘111A222B333C’ —> [‘111’, ‘A’, ‘222’, ‘B’, ‘333’, …

How to Split a String Between Numbers and Letters? Read More »

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 »