TransWikia.com

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

Stack Overflow Asked by Werner Germán Busch on January 7, 2021

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.

One Answer

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_]+'

Correct answer by sepp2k on January 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP