Backreferences in python ?

Jean-Paul Calderone exarkun at divmod.com
Mon Jan 23 10:22:32 EST 2006


On 23 Jan 2006 07:18:13 -0800, Pankaj <pankajgode at gmail.com> wrote:
>
> [snip]
>What i tried in python was::
>
>f = open( "./1.c", "r")
>fNew = open( "./1_new.c", "w")
>for l in f:
>        print l
>        lineno = lineno + 1
>        strToFind = "for\((.*)\;(.*)"
>
>        ##  For Converting int to string, i.e. line no. to string
>        lineNoClone = lineno
>
>        pattern = "printf(\"" + str( lineNoClone) + "\"),"
>
>        print pattern
>
>        strToReplace = "for\(" + pattern + "\1\;"
>
>        fNew.write( l.replace( strToFind, strToReplace) )
>
>        print l
>
>fNew.close()
>

str.replace() does not support regular expressions.  Take a look at the re module.

Jean-Paul



More information about the Python-list mailing list