iam using python regex extract values given string. string:
mystring.txt
sometext somemore text here 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 need extract course name , corresponding marks particular student. example, need course , marks myname
above string.
i tried:
re.findall(".*?course: (\w+).*?myname\s+(\d+).*?",buff,re.dotall)
but works if myname present under each course, not if myname missing in of course, in example string.
here output as: [('course1', '69'), ('course2', '60')]
but want achive is: [('course1', '69'), ('course4', '60')]
what correct regex 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)
.*?course: (\w+)(?:(?!\bcourse\b).)*myname\s+(\d+).*? ^^^^^^^^^^^^
you can try this.see demo.just use lookahead based quantifier search myname
before course
before it.
Comments
Post a Comment