Python equivalent of Perl e flag with regular expression
Peter Otten
__peter__ at web.de
Thu Oct 2 14:47:23 EDT 2008
Friedman, Jason wrote:
> I have lines that look like this:
> select column1, 'select' as type
> from table
> where column2 = 'foo'
>
> I want to return:
> SELECT column1, 'select' AS type
> FROM table
> WHERE column2 = 'foo'
>
> This is SQL with the keywords converted to uppercase. Note that the
> second "select" string is not a keyword and thus I do not want to
> convert it to uppercase. Thus, I don't think the string.replace()
> method will work for me.
>
> With Perl I might do something like this:
> $line =~ s/(select)/uc($1)/e;
> More generally:
> for $keyword in (@keyword) {
> $line =~ s/($keyword)/uc($1)/e;
> }
I think your perl code is broken. It mechanically replaces the first
occurence of the keyword. This fails e. g. for
"select 'from' as x, b as y from table;"
> How would I do this with Python?
Here's my attempt, but you won't get 100% reliability without a real parser.
import re
sql = "select 'x' as t, 'y''' as u, selected, 'from' as fromage from temp;"
def fix_sql(sql):
def sub(m):
kw = m.group(3)
if kw:
return kw.upper()
return m.group(1) or m.group(2)
return re.compile("""('.*?')|(".*?")|\\b(select|as|from)\\b""").sub(sub,
sql)
print fix_sql(sql)
Peter
More information about the Python-list
mailing list