Regex

A RegEx to Match Bitcoin Addresses

What regular expressions can be used to match Bitcoin addresses? A regular expression for validating Bitcoin addresses must check that the string is 26-35 characters long, start with “1” or “3” or “bc1” consists of uppercase or lowercase alphabetic and numeric characters, and ensure it doesn’t contain ambiguous characters. Not allowed are the uppercase letter …

A RegEx to Match Bitcoin Addresses Read More »

Python Regex to Return String Between Parentheses

Problem Formulation Given a string s. How to find the substring s’ between an opening and a closing parentheses? Consider the following examples: Input: ‘Learn Python (not C++)’ Output: ‘not C++’ Input: ‘function(a, b, c, d)’ Output: ‘a, b, c, d’ Input: ‘(a+(b+c))’ Output: ‘a+(b+c)’ Method 1: Slicing and str.find() The simplest way to extract …

Python Regex to Return String Between Parentheses Read More »

How To Remove All Non-Alphabet Characters From A String?

💡 Summary: This blog explores the steps to remove all non-alphabet characters from a given string. The ‘re’ module in Python provides regular expression operations, to process text. One uses these operations to manipulate text in strings. The compile() method in conjunction with the sub() method can remove all non-alphabet characters from a given string. Note: …

How To Remove All Non-Alphabet Characters From A String? Read More »

How to Escape Special Characters of a Python String with a Single Backslash?

The backslash escape character ‘\’ is a special Python string character that is usually followed by an alphabetic character. For example, the tabular whitespace ‘\t’ and newline ‘\n’. In regular expressions, you can use the single escape to remove the special meaning of regex symbols. For example, to match the dot or asterisk characters ‘.’ …

How to Escape Special Characters of a Python String with a Single Backslash? Read More »

Python endswith() Tutorial – Can We Use Regular Expressions?

While refactoring my Python code, I thought of the following question. Can You Use a Regular Expression with the Python endswith() Method? The simple answer is no because if you can use a regex, you won’t even need endswith()! Instead, use the re.match(regex, string) function from the re module. For example, re.match(“^.*(coffee|cafe)$”, tweet) checks whether …

Python endswith() Tutorial – Can We Use Regular Expressions? Read More »

You Cannot Use Python Regex in startswith(). Do This Instead.

I’m sitting in front of my computer refactoring Python code and have just thought of the following question: Can You Use a Regular Expression with the Python string.startswith() Method? The short answer is no. The string.startswith() method doesn’t allow regular expression inputs. And you don’t need it because regular expressions can already check if a …

You Cannot Use Python Regex in startswith(). Do This Instead. Read More »