Just to explain why you think search returned what you want, but findall n't?
the search returns an SRE_Match object that contains some information, such as:
string : the attribute contains the string passed to the search function.re : The REGEX object used in the search function.groups() : a list of lines captured by capture groups inside REGEX .group(index) : to retrieve the captured row by group using index > 0 .group(0) : return a string matching REGEX .
search stops when it detects that the first mechanism has created an SRE_Match object and, returning it, check this code:
import re s = r'abc123d' pattern = r'-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+' m = re.search(pattern, s) print(m.string)
findall behaves differently because it does not just stop when it finds the first mechanism that continues to extract to the end of the text, but if REGEX contains at least one capture group, findall does not return a matching string, but a captured string by capture groups:
import re s = r'abc123d , hello 3.1415926, this is my book' pattern = r'-?[0-9]+(\.[0-9]*)?|-?\.[0-9]+' m = re.findall(pattern, s) print(m)
the first element returned when the first mechanism was discovered when the '123' capture group only captured '' , but the second element was captured in the second match '3.1415926' the capture group corresponded to this part of '.1415926' .
If you want the returned string to match findall , you must make all capture groups () in your REGEX capture groups (?:) :
import re s = r'abc123d , hello 3.1415926, this is my book' pattern = r'-?[0-9]+(?:\.[0-9]*)?|-?\.[0-9]+' m = re.findall(pattern, s) print(m)
Charif dz
source share