Backreferences in python ?

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Jan 23 14:22:23 EST 2006


"Pankaj" <pankajgode at gmail.com> wrote in message
news:1138029493.503320.178550 at o13g2000cwo.googlegroups.com...
>
> I have something like below in perl and  i am searching for equivalent
> in python:
>
> ::: Perl :::
> ***********
> while( <FILEHANDLE> )
> {
>
>      line = $_;
>
>      pattern = "printf\( \"$lineNo \" \),";
>
>      line =~ s/"for(.*)\((*.)\;(.*)/for$1\($pattern$2\;$3/g;
> }
>
> This is used to
>
> search for :    for ( i = 0; i < 10; i++)
> Replace with:  for( printf( "10" ), i =0; i < 10; i++)
> Where 10 is the line no.
>

Here is a solution using pyparsing instead of re's.  You're already used to
re's from using Perl, so you may be more comfortable using that tool in
Python as well.  But pyparsing has some builtin features for pattern
matching, calling out to callback routines during parsing, and a lineno
function to report the current line number, all wrapped up in a simple
transformString method call.

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul


from pyparsing import Keyword,SkipTo,lineno,cStyleComment

# define grammar for a for statement
for_ = Keyword("for")
forInitializer = SkipTo(';').setResultsName("initializer")
forStmt = for_ + "(" + forInitializer + ';'

# ignore silly comments
forStmt.ignore(cStyleComment)

# setup a parse action that will insert line numbers
# parse actions are all called with 3 args:
# - the original string being parsed
# - the current parse location where the match occurred
# - the matching tokens
# if a value is returned from this function, transformString will
# insert it in place of the original content
def insertPrintStatement(st,loc,toks):
    lineNumber = lineno(loc,st)
    if toks[0]:
        return r'print("%d\n"), %s' % (lineNumber,toks[0])
    else:
        return r'print("%d\n")' % lineNumber
forInitializer.setParseAction(insertPrintStatement)

# transform some code
# this is how you would read in a whole file as a single string
#testdata = file(inputfilename).read()
# to read the entire file into a list of strings, do:
#testdata = file(inputfilename).readlines()
# for now, just fake some source code
testData = """
    for(i = 0; i <= 100; ++i)
    {
       /* some stuff */
    }

    for  (;;;)
    {
        /* do this forever */
    }

    /* this for has been commented out
    for(a = -1; a < 0; a++)
    */

"""

# use the grammar and the associated parse action to
# transform the source code
print forStmt.transformString(testData)

--------------------------
Gives:
    for(print("2\n"), i = 0; i <= 100; ++i)
    {
       /* some stuff */
    }

    for(print("7\n");;;)
    {
        /* do this forever */
    }

    /* this for has been commented out
    for(a = -1; a < 0; a++)
    */






More information about the Python-list mailing list