Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

why is my regular expression not matching? I keep getting errors. the first line

ID: 3891810 • Letter: W

Question

why is my regular expression not matching? I keep getting errors. the first line in the file is a=b+4

import re #def main ( ) : line 1 charlist = [] tokenlist[ outlist = [] analyzer-[ IDENT [a-zA-Z] ([a-z A-z] I 0 INT (0-9) *"], "MULTOP " : ["(('·* ' | '/')")"], "assign " : ["('''I ' /' I ' + term' "whitespace ["''*)' ] filein-input "Enter the file name) #outfile input ("Enter in the output file:) with open(filein) as f: lexlist = f. readlines() for i in lexlist charlist i for i in analyzer #outlist, append (i) #print (charlist) if re.match (analyzerli], i): analyzeri].append (line) linet 1 print (tokenlist) outlist.sort() print outlist)

Explanation / Answer

It might be the issue with for loops

You are taking the same variable i in the second loop as well. So effectively you are matching

Match this instead

re.match(analyzer[i], charlist)