I'm trying to match all numbers in a given body of text using re.findall() and convert them to integers. I know that something like [0-9]+
or [\d]+
should match any numbers in the string, however, my output splits numbers up individually (e.g. '125' becomes '1', '2', '5'.
Here's what I have:
import re
regex_list = []
sample = "Here are a bunch of numbers 7746 and 12 and 1929 and 8827 and 7 and 8837 and 128 now convert them"
for line in sample:
line = line.strip()
if re.findall('([0-9]+)', line):
regex_list.append(int(line))
print(regex_list)
Output:
[7, 7, 4, 6, 1, 2, 1, 9, 2, 9, 8, 8, 2, 7, 7, 8, 8, 3, 7, 1, 2, 8]
Desired Output:
[7746, 12, 1929, 8827, 7, 8837, 128]