Backreferences in python ?

Pankaj pankajgode at gmail.com
Mon Jan 23 12:16:53 EST 2006


My tries have with re have not yielded results::


{
      strToFind = 'for*;*'

        ## Converting int to string, i.e. line no. to string
        lineNoClone = lineno

        pattern = "printf(\"" + str( lineNoClone) + "\"),"

         regObj = re.compile( strToFind)

        m =  regObj.search( l)


        if ( m != None ) :
                subStrPattern1_hasInitialization = "\1"
                #m.group(1)

                subStrPattern2_hasRestTillEnd = "\2"
                #m.group(2)

                strToReplace = "for(" + pattern +
subStrPattern1_hasInitialization + ";" + subStrPattern2_hasRestTillEnd
                fNew.write( regObj.sub( strToFind, strToReplace ) )
        else:
                fNew.write( l)
}


Here problem is , i am not getting backreferences using \1 and \2

The string :   for( i =0; i < 10; i++)

is getting replace by: for *;* {

I don't believe this,  they have given that \1 and \2 store
backreferences, then where r they??




More information about the Python-list mailing list