-1

Why is my code extracting individual digits in a number instead of the number (i.e. "300" is extracted as 3, then 0, then another 0)?

import re
open_file = open("regex_sum_42.txt")
read_file = open_file.read()
for line in read_file:
    line = line.rstrip()
    num_list = re.findall(".*([0-9])+",line)
    if len(num_list)>0:
        print num_list
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Veracity
  • 1
  • 1
  • There are three problems with your code: 1) The `+` needs to go _inside_ the group like so: `([0-9]+)`, otherwise the group only captures a single digit. 2) The `.*` in front of the capture group will consume all digits but the last one. You need to make this non-greedy: `.*?([0-9]+)`. 3) You're iterating through the text character by character, not line by line. – Aran-Fey May 11 '16 at 15:24
  • This was my final code: import re full_num_list = [] open_file = open("regex_sum_245272.txt") read_file = open_file.read() print sum(map(int,re.findall("([0-9]+)",read_file))) – Veracity May 11 '16 at 23:46

1 Answers1

0

You can just do:

re.findall(r"\d+", line)
heemayl
  • 39,294
  • 7
  • 70
  • 76