Python equivalent of Perl e flag with regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Friedman, Jason

    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. OppenheimerFund s may, at its sole discretion, monitor, review, retain and/or disclose the content of all email communications.
    =============== =============== =============== =============== =============== ===

  • Peter Otten

    #2
    Re: Python equivalent of Perl e flag with regular expression

    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|f rom)\\b""").sub (sub,
    sql)

    print fix_sql(sql)

    Peter

    Comment

    • George Sakkis

      #3
      Re: Python equivalent of Perl e flag with regular expression

      On Oct 2, 1:06 pm, "Friedman, Jason" <jfried...@oppe nheimerfunds.co m>
      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(lambd a match: match.group().u pper(), sql)
      SELECT column1, 'SELECT' AS type
      FROM table
      WHERE column2 = 'foo'

      George

      Comment

      • MRAB

        #4
        Re: Python equivalent of Perl e flag with regular expression

        On Oct 2, 6:06 pm, "Friedman, Jason" <jfried...@oppe nheimerfunds.co m>
        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'"

        Comment

        Working...