Home >Backend Development >Python Tutorial >Why Does `re.findall` Return an Empty List When Matching Numbers in Python?

Why Does `re.findall` Return an Empty List When Matching Numbers in Python?

Barbara Streisand
Barbara StreisandOriginal
2024-12-27 01:20:10785browse

Why Does `re.findall` Return an Empty List When Matching Numbers in Python?

re.findall Behaves Inconsistently

When attempting to use re.findall to match numerical values in a string, some users have noticed unexpected behavior. While re.search accurately retrieves matches, re.findall returns an empty list. This apparent disparity can be attributed to the following factors:

Capturing Groups and re.findall

One key point to remember is that re.findall acquires captured text if the regex pattern contains capturing groups. As defined in the reference, if a pattern includes capturing groups, re.findall returns a list of groups, potentially in the form of tuples for patterns with multiple groups. Notably, even empty matches are included in the output, unless they immediately precede another match.

Non-Capturing Groups and Literal Escaping

In the provided example, the specific issue stems from the use of \ within r'' string literals. This construct attempts to match a literal , rather than the intended meaning of matching any single character (other than a newline). To correctly match numerical values, the pattern should be modified to:

-?\d*\.?\d+

This pattern includes the following capture groups:

  • -? - Optional minus sign
  • d* - Optional digits
  • . - Optional decimal separator
  • d - One or more digits

Demonstration

Here is an IDEONE demonstration of the revised pattern:

import re
s = r'abc123d, hello 3.1415926, this is my book'
pattern = r'-?\d*\.?\d+'
L = re.findall(pattern, s)
print(L)

This pattern correctly retrieves the expected list of numerical matches: ['123', '3.1415926'].

By considering the nature of capturing groups and the appropriate usage of literal escaping, developers can ensure that re.findall functions as intended in their scripts.

The above is the detailed content of Why Does `re.findall` Return an Empty List When Matching Numbers in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn