Search code examples
pythonply

How to use flags with python's regular expressions in string notation?


I need to use PLY for a parser, and PLY forces you to write regular expressions in string ntoation inside a token definition. For example (from the docs):

# A regular expression rule with some action code
def t_NUMBER(t):
    r'\d+'
    t.value = int(t.value)
    return t

# Define a rule so we can track line numbers
def t_newline(t):
    r'\n+'
    t.lexer.lineno += len(t.value)

I was wondering how to write more complex regular expressions using this notation, specifically how to use the IGNORECASE flag for the RE.


Solution

  • To enable the ignore case flag, you can use (?i) at the beginning of the regular expression. For example:

    def t_id(t):
      r'(?i)[a-z_][a-z0-9_]+'