Search code examples
pythonmatchingpython-re

How to describe this using python re?


I creating esoteric programming language and I want to realize this structure to print text:

   !(some-text)

This structure must write text in parentheses and end up with ).

But for some reason this structure end up only when find newline. I think it happened 'cause dot matching any symbol except \n. I tried to create condition using (?!...) but this wasn't working too. Eventually I don't know how to match all symbols without \n and ).

Below I have attached the full code of the token table so that you find errors in it and suggest ways to fix them. Thanks in advance for any help!

token_table = [
(r'[ \t\n]+', None),
(r'!\((?!\)).*\)', 'PRINTSTR'), # This is not working
(r'[-]?[0-9]+', 'INT'),
(r'(?!!|\?|\(|\)|:|<|>|\+).*', None),
(r'!', 'PASS'),
(r'\?', 'IF'),
(r'\(', 'LPAR'),
(r'\)', 'RPAR'),
(r'>', 'INPUT'),
(r'<', 'PRINT'),
(r':', 'GOTO'),
(r'\+', 'ADD'), ]

Solution

  • Don't use ., use [^)] to match any character other than ). This will include newlines.

    (r'!\([^)]*\)', 'PRINTSTR')