The source string is:
# Python 3.4.3
s = r'abc123d, hello 3.1415926, this is my book'
and here is my pattern:
pattern = r'-?[0-9]+(\\.[0-9]*)?|-?\\.[0-9]+'
however, re.search
can give me correct result:
m = re.search(pattern, s)
print(m) # output: <_sre.SRE_Match object; span=(3, 6), match='123'>
re.findall
just dump out an empty list:
L = re.findall(pattern, s)
print(L) # output: ['', '', '']
why can't re.findall
give me the expected list:
['123', '3.1415926']
You dont need to
escape
twice when you are usingraw mode
.Output:
['123', '3.1415926']
Also the return type will be a list of
strings
.If you want return type asintegers
andfloats
usemap
Output:
[123, 3.1415926]
There are two things to note here:
re.findall
returns captured texts if the regex pattern contains capturing groups in itr'\\.'
part in your pattern matches two consecutive chars,\
and any char other than a newline.See
findall
reference:Note that to make
re.findall
return just match values, you may usually(a(b)c)
->abc
)(
with(?:
) unless there are backreferences that refer to the group values in the pattern (then see below)re.finditer
instead ([x.group() for x in re.finditer(pattern, s)]
)In your case,
findall
returned all captured texts that were empty because you have\\
withinr''
string literal that tried to match a literal\
.To match the numbers, you need to use
The regex matches:
-?
- Optional minus sign\d*
- Optional digits\.?
- Optional decimal separator\d+
- 1 or more digits.See demo
Here is IDEONE demo: