473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Python equivalent of Perl e flag with regular expression

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?

------------------------------------------------------------------------------
This e-mail transmission may contain information that is proprietary, privileged and/or confidential and is intended exclusively for the person(s)to whom it is addressed. Any use, copying, retention or disclosure by any person other than the intended recipient or the intended recipient's designees is strictly prohibited. If you are not the intended recipient or their designee, please notify the sender immediately by return e-mail and delete all copies. OppenheimerFunds may, at its sole discretion, monitor, review, retain and/or disclose the content of all email communications.
================================================== ============================

Oct 2 '08 #1
3 1460
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
Oct 2 '08 #2
On Oct 2, 1:06 pm, "Friedman, Jason" <jfried...@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.

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:
>>import re
keywords = 'select from where as'.split()
regex = re.compile('|'.join(r'\b%s\b' % re.escape(k) for k in keywords), re.I)
sql = """
select column1, 'select' as type
from table
where column2 = 'foo'
"""
>>print regex.sub(lambda match: match.group().upper(), sql)
SELECT column1, 'SELECT' AS type
FROM table
WHERE column2 = 'foo'

George
Oct 2 '08 #3
On Oct 2, 6:06*pm, "Friedman, Jason" <jfried...@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'"
Oct 3 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
68
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
4
by: John K Masters | last post by:
I am currently working my way through Jeffrey Friedl's book Mastering Regular Expressions. Great book apart from the fact it uses Perl for the examples. One particular expression that interests...
1
by: skip | last post by:
JasonWith Perl I might do something like this: Jason$line =~ s/(select)/uc($1)/e; ... JasonHow would I do this with Python? I'm sure there are plenty of ways to skin this particular cat, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.