Backreferences in python ?

Giovanni Bajo noway at sorry.com
Mon Jan 23 21:07:35 CET 2006


Pankaj wrote:

>>>> 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.


import re
import fileinput

for L in fileinput.input(inplace=True):
    pattern = 'printf("%d"),' % input.filelineno()
    L = re.sub(r"for(.*)\((*.)\;(.*)", r"for\1\(%s\2;\3" % pattern, L)
    print L,

or something
-- 
Giovanni Bajo





More information about the Python-list mailing list