Python equivalent of Perl e flag with regular expression

  • Thread starter Friedman, Jason
  • Start date
F

Friedman, Jason

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;
}

How would I do this with Python?
 
P

Peter Otten

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
 
G

George Sakkis

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;

}

Are you sure that this version returns the desired results ? How does
perl know not to much the keyword within the quotes ?
How would I do this with Python?

Leaving aside the fact that regexps are not the right tool to check
whether a string is within quotes or not, you can use re.sub() and
pass a callable instead of a replacement string:
select column1, 'select' as type
from table
where column2 = 'foo'
"""SELECT column1, 'SELECT' AS type
FROM table
WHERE column2 = 'foo'

George
 
M

MRAB

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.
[snip]

FYI, yhe replace method can take a third argument, which is the
maximum number of replacements to do:
"SELECT column1, 'select' as type from table where column2 = 'foo'"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top