re.findall Behaves Anomalous
Dilemma:
In a Python script using Python 3.4.3, the re.search method provides the anticipated result when applied to a particular source string using a specified pattern. However, re.findall inexplicably returns an empty list for the same pattern and source string.
Exploration:
Curiously, the re.findall method behaves unexpectedly despite appearing to fulfill the requirements for extracting matches from a string based on a pattern.
Resolution:
Upon careful examination, it becomes evident that two factors contribute to this peculiar behavior:
Rectified Pattern:
To alleviate this issue and successfully extract matches, the following rectified pattern should be employed:
-?\d*\.?\d+
Explanation:
Demonstration:
Utilizing the modified pattern, the re.findall method now accurately retrieves the anticipated list of matches:
['123', '3.1415926']
The above is the detailed content of Why Does `re.findall` Return an Empty List While `re.search` Works Correctly with the Same Pattern and String in Python 3.4.3?. For more information, please follow other related articles on the PHP Chinese website!