Regex

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 »

Regex to Match Dollar Amounts with Optional Cents

Problem: How to match dollar amounts in a given string? If cent amounts are given, those should match as well. Solution: You can use regular expression pattern ‘(\$[0-9]+(.[0-9]+)?)’ to find all numbers that start with a dollar symbol, followed by an arbitrary number of numerical digits, followed by an optional decimal point and an arbitrary …

Regex to Match Dollar Amounts with Optional Cents Read More »

How To Split A String And Keep The Separators?

Summary: To split a string and keep the delimiters/separators you can use one of the following methods: Use a regex module and the split() method along with \W special character. Use a regex module and the split() method along with a negative character set [^a-zA-Z0-9]. Use a regex module and the split() method along with …

How To Split A String And Keep The Separators? Read More »

How To Extract Numbers From A String In Python?

Summary: To extract numbers from a given string in Python you can use one of the following methods: Use the regex module. Use split() and append() functions on a list. Use a List Comprehension with isdigit() and split() functions. Use the num_from_string module. Extracting digits or numbers from a given string might come up in …

How To Extract Numbers From A String In Python? Read More »

Asterisk Operator

What is the Asterisk / Star Operator (*) in Python?

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and …

What is the Asterisk / Star Operator (*) in Python? Read More »

How to Search and Replace a Line in a File in Python?

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file? Example: Let’s consider the following example. Text in the file before replacing a line: There was an idea to bring together a group of remarkable people to see if we could become something more. …

How to Search and Replace a Line in a File in Python? Read More »

Python One Line Regex Match

Summary: To match a pattern in a given text using only a single line of Python code, use the one-liner import re; print(re.findall(pattern, text)) that imports the regular expression library re and prints the result of the findall() function to the shell. Problem: Given a string and a regular expression pattern. Match the string for …

Python One Line Regex Match Read More »