i have a file similar to that shown below- is it possible to perform a regex expression
text1 769,230,123
text2 70
text3 213,445
text4 24,356
text5 1,2,4
to give output as shown here?
['769','230','123']
['70']
['213','445']
My current code is as follows:
with open(filename,'r') as output:
for line in output:
a = line
a = a.strip()
#regex.compile here
print regex.findall(a)
Any help or direction would be greatly useful to me. Thank you
The following regex will extract the comma separated numbers from the line, and then we can apply split(',') in order to extract the numbers:
import re
line = "text1 769,230,123"
mat = re.match(r'.*? ([\d+,]+).*', line)
nums = mat.group(1).split(',')
for num in nums:
print num
OUTPUT
769
230
123
It looks like you could just findall number sequences:
regex = re.compile("[ ,]([0-9]+)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With