Python equivalent of Perl e flag with regular expression

MRAB google at mrabarnett.plus.com
Fri Oct 3 17:47:34 EDT 2008


On Oct 2, 6:06 pm, "Friedman, Jason" <jfried... at oppenheimerfunds.com>
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.
>
[snip]

FYI, yhe replace method can take a third argument, which is the
maximum number of replacements to do:

>>> query = "select column1, 'select' as type from table where column2 = 'foo'"
>>> query.replace("select", "SELECT", 1)
"SELECT column1, 'select' as type from table where column2 = 'foo'"



More information about the Python-list mailing list