Iam using python regex to extract certain values from a given string. This is my string:
mystring.txt
sometext
somemore text here
some other text
course: course1
Id Name marks
____________________________________________________
1 student1 65
2 student2 75
3 MyName 69
4 student4 43
course: course2
Id Name marks
____________________________________________________
1 student1 84
2 student2 73
8 student7 99
4 student4 32
course: course4
Id Name marks
____________________________________________________
1 student1 97
3 MyName 60
8 student6 82
and I need to extract the course name and corresponding marks for a particular student. For example, I need the course and marks for MyName from the above string.
I tried:
re.findall(".*?course: (\w+).*?MyName\s+(\d+).*?",buff,re.DOTALL)
But this works only if MyName is present under each course, but not if MyName is missing in some of the course, like in my example string.
Here I get output as: [('course1', '69'), ('course2', '60')]
but what actually what I want to achive is: [('course1', '69'), ('course4', '60')]
what would be the correct regex for this?
#!/usr/bin/python
import re
buffer_fp = open("mystring.txt","r+")
buff = buffer_fp.read()
buffer_fp.close()
print re.findall(".*?course: (\w+).*?MyName\s+(\d+).*?",buff,re.DOTALL)