How to get PLY to ignore case of a regular expression?
Asked Answered
P

2

6

I'm working on a simple translator from SQL INSERT statements to a dataset XML file to be used with DbUnit.

My current definition looks like this:

def t_INSERT(token):
    r'INSERT\s+INTO'
    return token

Now, I want to support case insensitive commands of SQL, for example, accept all of INSERT INTO, Insert Into, insert into and iNsErT inTO as the same thing.

I wonder if there is a way to PLY use re.I so that it will ignore the case, or yet another alternative to write the rule that I'm not familiar with.

Psychosexual answered 10/5, 2012 at 16:4 Comment(0)
C
9

You can inject flags into regexp using (?) syntax. Try '(?i)INSERT\s+INTO', it adds the flag to ignore case.

Canales answered 10/5, 2012 at 16:35 Comment(0)
B
2

Internally, lex.py uses the re module to do its pattern matching.
If you need to supply optional flags to the re.compile() function, use the reflags option to lex. For example:

lex.lex(reflags=re.UNICODE)

This information is extracted from documentation sections 4.3 and 4.20
In your case, you can pass re.IGNORECASE to lexer:

import re
lex.lex(reflags=re.IGNORECASE) 

Your code is more readable if you pass flags this way.

Bove answered 3/11, 2017 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.