, . , , , () , (b) .
, :
import re
rgx = re.compile(r'\-?\d+')
the_sum = 0
with open("regex_sum_42.txt") as handle:
for line in handle:
the_sum += sum(int(x) for x in rgx.findall(line))
print(the_sum)
, , , , . . \-?, , -2, . :
445833
, . , , . , , :
import re
rgx = re.compile(r'\-?\d+(?:\.\d*)?')
the_sum = 0
with open("regex_sum_42.txt") as handle:
for line in handle:
the_sum += sum(float(x) for x in rgx.findall(line))
print(the_sum)
, non-capture (?:..) , findall ( , ). :
445833.0
, 'http://www.py4e.com/code3/', '4' '3' . '\b', :
import re
rgx = re.compile(r'\b\-?\d+(?:\.\d*)?\b')
the_sum = 0
with open("regex_sum_42.txt") as handle:
for line in handle:
the_sum += sum(float(x) for x in rgx.findall(line))
print(the_sum)
:
445822.0
, 11.