Python Regex – ¿Cómo contar el número de coincidencias?

Para contar un patrón de expresión regular varias veces en una cadena dada, usa el método len(re.findall(pattern, string)) que devuelve el número de subcadenas coincidentes o len([*re.finditer(pattern, text)]) que desempaqueta todas las subcadenas coincidentes en una lista y también devuelve la longitud de la misma. Hace unas horas, escribí una expresión regular en Python que … Read more

Replace Last Substring Occurrence in Python String

Problem Formulation Given a string, a substring, and a replacement string in Python. String s Substring sub Replacement string repl How to find and replace the last occurrence of sub with the replacement repl in the Python string s? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: … Read more

Find Index of Last Substring Occurrence in Python String

Problem Formulation Given a string and a substring in Python. How to find the index of the last occurrence of the substring in Python? Let’s have a look at a couple of examples to thoroughly understand the problem: Example 1: string = ‘fifi’ substring = ‘fi’ result: 2 Example 2: string = ‘hello’ substring = … Read more

Do You Need to Escape a Dot in a Python Regex Character Class?

Question Recap the following regular expression concepts (more details in the article below): The dot character . in regular expressions matches any character excluding the newline character. For example, the pattern ‘c.t’ will match the strings ‘cat’, ‘cut’, or ‘czt’. The character class [ ] is a set of characters: if you use it in … Read more

Python Print Without Quotes

Problem Formulation In Python’s interactive mode, each line is assumed to be an expression that is evaluated. The return value is provided to the user. Thus, if you evaluate a string expression or call a function or an operation that returns a string, the output will display quotes around the string to tell the user … Read more

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 … 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 … 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: … Read more