Iam using python regex to extract specific values ββfrom a given string. This is my line:
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 labels for a particular student. For example, I need a course and labels for MyName from the line above.
I tried:
re.findall(".*?course: (\w+).*?MyName\s+(\d+).*?",buff,re.DOTALL)
But this only works if MyName is present in each course, but not if MyName is absent in some courses, as in my example line.
Here I get the output as: [('course1', '69'), ('course2', '60')]
but actually I want to achieve: [('course1', '69'), ('course4', '60')]
What is the correct regular expression 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)
source share