简体   繁体   中英

Matching with regex line for line

I am working on a fun little language using regex matching lines in a file. Here is what I have so far:

import re

code=open("code.txt", "r").read()

outputf=r'output (.*)'
inputf=r'(.*) = input (.*)'
intf=r'int (.*) = (\d)'
floatf=r'float (.*) = (\d\.\d)'
outputq=re.match(outputf, code)
if outputq:
    print "Executing OUTPUT query"
    exec "print %s" %outputq.group(1)

inputq=re.match(inputf, code)
if inputq:
    print "Executing INPUT query"
    exec "%s=raw_input(%s)"%(inputq.group(1), inputq.group(2))

intq=re.match(intf, code)
if intq:
    exec "%s = %s"%(intq.group(1), intq.group(2))
    exec "print %s"%(intq.group(1))
else:
    print "Invalid syntax"

The code works in matching say:

int x = 1

But it will only match the first line and stop matching and ignore the rest of the code that I want to match. How can I match every line in the file to my regex definitions?

.read() reads as one line, use .split("\\n") on the .read() code or use .readlines() .

Then iterate over the lines and test for your commands. At the moment you take the whole code as one single line. You want to check all lines line by line.

EDIT:

for that, create a function

then read lines with readlines()

And finally iterate over lines, using the function on lines

Like that:

import re

outputf=r'output (.*)'
inputf=r'(.*) = input (.*)'
intf=r'int (.*) = (\d)'
floatf=r'float (.*) = (\d\.\d)'

def check_line(line):
    outputq=re.match(outputf, line)
    if outputq:
        print ("Executing OUTPUT query")
        exec ("print (%s)" % outputq.group(1))

    inputq=re.match(inputf, line)
    if inputq:
        print ("Executing INPUT query")
        exec ("%s=raw_input(%s)"%(inputq.group(1), inputq.group(2)))

    intq=re.match(intf, line)
    if intq:
        exec ("%s = %s"%(intq.group(1), intq.group(2)))
        exec ("print (%s)"%(intq.group(1)))
    else:
        print ("Invalid syntax")


code=open("code.txt", "r").readlines()

for line in code:
    check_line(line)

This code will still return an error, which has nothing to do with the issue tho, think about if you do the assigning of the variable correctly.

You're using re.match() which means that your regex has to match the whole string (which in this case is the whole file). If you iterate over each line in the file, then .match() will work. Alternatively you might want to look at re.search() , re.findall() and other similar alternatives.

It looks like your code needs to iterate over the lines in the file: How to iterate over the file in python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM